<?php

namespace app\wapapi\controller;

use addons\blockchain\model\VslBlockChainRecordsModel;
use addons\blockchain\model\VslEosOrderPayMentModel;
use addons\blockchain\service\Block;
use addons\presell\service\Presell;
use data\model\AddonsConfigModel;
use data\model\DistrictModel;
use data\model\VslMemberRechargeModel;
use data\model\VslOrderExpressCompanyModel;
use data\model\VslOrderModel;
use data\model\UserModel;
use data\model\VslOrderPaymentModel;
use data\model\VslPresellModel;
use data\model\VslMemberCardModel;
use data\service\Member;
use \data\service\Order as OrderService;
use data\service\Order\Order as OrderBusiness;
use data\service\UnifyPay;
use addons\shop\model\VslShopModel;
use addons\coupontype\server\Coupon;
use think\Cookie;
use think\Db;
use think\Log;
use think\config;
use addons\seckill\server\Seckill as SeckillServer;
use data\service\Goods as GoodsService;
use addons\store\server\Store;
use think\Session;
use addons\groupshopping\server\GroupShopping;
use data\model\VslExpressCompanyShopRelationModel;
use data\service\Address;
use data\service\Order\OrderStatus;
/**
 * 商品控制器
 */


class Order extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        if (!IS_CLI) {
            if (!getUserId()) {
                echo json_encode(['code' => LOGIN_EXPIRE, 'message' => '登录信息已过期，请重新登陆'], JSON_UNESCAPED_UNICODE);
                exit;
            }
            $this->uid = getUserId();
        }

    }

    /**
     * 订单列表
     *
     */
    public function orderList()
    {
        $page_index = request()->post('page_index', 1);
        $page_size = request()->post('page_size') ?: PAGESIZE;
        $order_status = request()->post('order_status');
        $search_text = request()->post('search_text');
        $condition['is_deleted'] = 0; // 未删除订单
        $condition['buy_type'][] = ['NEQ',2];// 不显示渠道商自提订单
        $condition['buyer_id'] = $this->uid;
        $condition['website_id'] = $this->website_id;
        if (is_numeric($search_text)) {
            $condition['order_no'] = ['LIKE', '%' . $search_text . '%'];
        } elseif ( strstr($search_text, 'DH') ) {
            $condition['order_no'] = ['LIKE', '%' . $search_text . '%'];
        } elseif (!empty($search_text)) {
            $condition['or'] = true;
            $condition['shop_name'] = ['LIKE', '%' . $search_text . '%'];
            $condition['goods_name'] = ['LIKE', '%' . $search_text . '%'];
        }

        if ($order_status != '') {
            // $order_status 1 待发货
            if ($order_status == 1) {
                // 订单状态为待发货实际为已经支付未完成还未发货的订单
                $condition['shipping_status'] = 0; // 0 待发货
                $condition['pay_status'] = 2; // 2 已支付
                $condition['order_status'][] = ['neq', 4]; // 4 已完成
                $condition['order_status'][] = ['neq', 5]; // 5 关闭订单
                $condition['order_status'][] = ['neq', -1]; // -1 售后订单
            } elseif ($order_status == -1) {
                //售后订单
                //$condition['order_status'] = ['<', 0];
                $condition['refund_status'] = [-1, -3, 1, 2, 3, 4, 5, 6];
            } elseif($order_status == 0) {    //调试，记得换回
                //待付款
                $condition['order_status'] = 0;
            } elseif ($order_status == -2){
                // 待评价
                $condition['is_evaluate'] = 0;
                $condition['order_status'] = ['in', [3, 4]];
            } else {
                $condition['order_status'] = $order_status;
            }
        }
        $order_service = new OrderService();
        $list = $order_service->getOrderList($page_index, $page_size, $condition, 'create_time DESC');
        $order_list = [];
        foreach ($list['data'] as $k => $order) {
            $order_list[$k]['order_id'] = $order['order_id'];
            $order_list[$k]['offline_pay'] = $order['offline_pay'];
            $order_list[$k]['order_no'] = $order['order_no'];
            if(!empty($order['presell_id']) && $this->is_presell){
                $order_list[$k]['out_order_no'] = $order['out_trade_no'];
                $order_list[$k]['out_trade_no_presell'] = $order['out_trade_no_presell'];
            }else{
                $order_list[$k]['out_order_no'] = $order['out_trade_no'];
                $order_list[$k]['out_trade_no_presell'] = '';
            }
            $order_list[$k]['shop_id'] = $order['shop_id'];
            $order_list[$k]['shop_name'] = $order['shop_name'] ?: '';
            $order_list[$k]['pay_type_name'] = $order['pay_type_name'];
            if($order['presell_id'] !=0 && $order['money_type'] == 2 && $this->is_presell){//付完尾款了
                $presell_mdl = new VslPresellModel();
                $presell_info = $presell_mdl->getInfo(['id'=>$order['presell_id']],'*');
                $order_list[$k]['presell_status'] = 2;//已付尾款
                $order_list[$k]['pay_type_name']= OrderStatus::getPayType($order['payment_type']) .'+'. OrderStatus::getPayType($order['payment_type_presell']) ;
                if($order['payment_type']==16 || $order['payment_type']==17){
                    if($order['payment_type_presell']!=16 && $order['payment_type_presell']!=17){
                        $block = new VslBlockChainRecordsModel();
                        $block_info = $block->getInfo(['data_id'=>$order['out_trade_no']],'*');
                        if($block_info && $block_info['from_type']==4){
                            $order_list[$k]['order_real_money'] = floatval($block_info['cash']).'ETH + ¥ '.$order['final_money'];
                        }else if($block_info && $block_info['from_type']==8){
                            $order_list[$k]['order_real_money'] = floatval($block_info['cash']).'EOS + ¥ '.$order['final_money'];
                        }
                        $order_list[$k]['order_money'] = $order['order_money'];
                    }
                    if($order['payment_type_presell']==16 || $order['payment_type_presell']==17){
                        $block = new VslBlockChainRecordsModel();
                        $block_info = $block->getInfo(['data_id'=>$order['out_trade_no']],'*');
                        $block_info1 = $block->getInfo(['data_id'=>$order['out_trade_no_presell']],'*');
                        if($block_info && $block_info['from_type']==4 && $block_info1 && $block_info1['from_type']==4){
                            $order_list[$k]['order_real_money'] = floatval($block_info['cash']).'ETH +'.$block_info1['cash'].'ETH';
                        }else if($block_info && $block_info['from_type']==4 && $block_info1 && $block_info1['from_type']==8){
                            $order_list[$k]['order_real_money'] = floatval($block_info['cash']).'ETH +'.$block_info1['cash'].'EOS';
                        }else if($block_info && $block_info['from_type']==8 && $block_info1 && $block_info1['from_type']==4){
                            $order_list[$k]['order_real_money'] = floatval($block_info['cash']).'EOS +'.$block_info1['cash'].'ETH';
                        }else if($block_info && $block_info['from_type']==8 && $block_info1 && $block_info1['from_type']==8){
                            $order_list[$k]['order_real_money'] = floatval($block_info['cash']).'EOS +'.$block_info1['cash'].'EOS';
                        }
                        $order_list[$k]['order_money'] = $order['order_money'];
                    }
                }else if($order['payment_type_presell']==16 || $order['payment_type_presell']==17){
                    if($order['payment_type']!=16 && $order['payment_type']!=17){
                        $block = new VslBlockChainRecordsModel();
                        $block_info1 = $block->getInfo(['data_id'=>$order['out_trade_no_presell']],'*');
                        if($block_info1 && $block_info1['from_type']==4){
                            $order_list[$k]['order_real_money'] = '¥ '.$order['pay_money'] .'+'.$block_info1['cash'].'ETH';
                        }else if($block_info1 && $block_info1['from_type']==8){
                            $order_list[$k]['order_real_money'] = '¥ '.$order['pay_money'] .'+'.$block_info1['cash'].'EOS';
                        }
                        $order_list[$k]['order_money'] = $order['order_money'];
                    }
                }else{
                    $order_list[$k]['order_real_money'] = '¥ '.$order['order_money'];
                    $order_list[$k]['order_money'] = $order['order_money'];
                }
            }elseif($order['presell_id'] !=0 && $order['money_type'] == 0 && $this->is_presell){//预售未付定金
                $order_list[$k]['presell_status'] = 0;//待付定金
                $order_list[$k]['order_money'] = $order['pay_money'];
                $order_list[$k]['order_real_money'] = '¥ '.$order['order_money'];
            }elseif($order['presell_id'] !=0 && $order['money_type'] == 1 && $this->is_presell){//预售付完定金，待付尾款
                $order_list[$k]['order_money'] = $order['final_money'];
                $order_list[$k]['order_real_money'] = '¥ '.$order['final_money'];
                $order_list[$k]['presell_status'] = 1;//待付尾款
            }else{
                $order_list[$k]['order_money'] = $order['order_money'];
                if($order['payment_type']==16){
                    $order_list[$k]['order_real_money'] = $order['coin'].'ETH';
                }elseif($order['payment_type']==17){
                    $order_list[$k]['order_real_money'] = $order['coin'].'EOS';
                }else{
                    $order_list[$k]['order_real_money'] = '¥ '.$order['order_money'];
                }
            }
            $order_list[$k]['order_point'] = $order['order_point'];
            $order_list[$k]['order_type'] = $order['order_type'];
            $order_list[$k]['order_status'] = $order['order_status'];
            $order_list[$k]['card_store_id'] = $order['card_store_id'];
            if(!empty($order['status_name'])){
                $order_list[$k]['status_name'] = $order['status_name'];
            }
            $order_list[$k]['is_evaluate'] = $order['is_evaluate'];
            if(isset($order['member_operation'])){
                $order_list[$k]['member_operation'] = array_merge($order['member_operation'], [['no' => 'detail', 'name' => '订单详情']]);
                //如果已付定金，显示付尾款按钮
                if($order['money_type']==1 && $order['pay_status']==0 && $order['order_status']!=3 && $order['order_status']!=5){
                    $order_list[$k]['member_operation'] = array(['no' => 'last_money','name' => '付尾款'], ['no' => 'detail', 'name' => '订单详情']);
                }
            }
            if($order['payment_type']==16 || $order['payment_type']==17){
                $block = new VslBlockChainRecordsModel();
                $block_status = $block->getInfo(['data_id'=>$order['out_trade_no']],'status')['status'];
                if($block_status==0){
                    $order_list[$k]['status_name'] = '链上处理中';
                    $order_list[$k]['member_operation'] = array_merge([], [['no' => 'detail', 'name' => '订单详情']]);
                }
            }
            if($order['payment_type_presell']==16 || $order['payment_type_presell']==17){
                $block = new VslBlockChainRecordsModel();
                $block_status = $block->getInfo(['data_id'=>$order['out_trade_no_presell']],'status')['status'];
                if($block_status==0){
                    $order_list[$k]['status_name'] = '链上处理中';
                    $order_list[$k]['member_operation'] = array_merge([], [['no' => 'detail', 'name' => '订单详情']]);
                }
            }
            $order_list[$k]['promotion_status'] = ($order['promotion_money'] + $order['coupon_money'] > 0) ?: false;
            foreach ($order['order_item_list'] as $key_sku => $item) {
                $order_list[$k]['order_item_list'][$key_sku]['order_goods_id'] = $item['order_goods_id'];
                $order_list[$k]['order_item_list'][$key_sku]['goods_id'] = $item['goods_id'];
                $order_list[$k]['order_item_list'][$key_sku]['sku_id'] = $item['sku_id'];
                $order_list[$k]['order_item_list'][$key_sku]['goods_name'] = $item['goods_name'];
                $order_list[$k]['order_item_list'][$key_sku]['price'] = $item['price'];
                $order_list[$k]['order_item_list'][$key_sku]['goods_point'] = $item['goods_point'];
                $order_list[$k]['order_item_list'][$key_sku]['num'] = $item['num'];
                $order_list[$k]['order_item_list'][$key_sku]['pic_cover'] = getApiSrc($item['picture']['pic_cover']);
                $order_list[$k]['order_item_list'][$key_sku]['spec'] = $item['spec'];
                $order_list[$k]['order_item_list'][$key_sku]['status_name'] = $item['status_name'];
                $order_list[$k]['order_item_list'][$key_sku]['refund_status'] = $item['refund_status'];

                $order_list[$k]['order_item_list'][$key_sku]['member_operation'] = $item['member_operation'] ? $item['member_operation'] : [];
                //判断是否是预售
                if(!empty($order['presell_id']) && $this->is_presell){
                    $presell = new Presell();
                    $presell_info = $presell->get_presell_by_sku($order['presell_id'],$item['sku_id']);
                    $order_list[$k]['pay_start_time'] = $presell_info['pay_start_time'];
                    $order_list[$k]['pay_end_time'] = $presell_info['pay_end_time'];
//                    $order_list[$k]['pay_money'] = $item['num']*($presell_info['allmoney']-$presell_info['firstmoney']) + $order['shipping_money'];
                }
            }
            $order_list[$k]['can_presell_pay'] = '';
            $order_list[$k]['can_presell_pay_reason'] = '';
            // 当订单需要进行整单售后时，这个字段取订单商品第一个商品的售后状态（目前正确情况，所有商品的refund_status一样），用于判断整单售后操作
            $order_list[$k]['order_refund_status'] = 0;
            if($order_list[$k]['order_item_list']){
                $order_list[$k]['order_refund_status'] = reset($order_list[$k]['order_item_list'])['refund_status'];
            }
            $order_list[$k]['unrefund'] = $order['unrefund'];
            $order_list[$k]['unrefund_reason'] = $order['unrefund_reason'];
            if(!empty($order['presell_id']) && $this->is_presell){
                if (time()<=$presell_info['pay_end_time'] && time()>=$presell_info['pay_start_time']) {
                    $order_list[$k]['can_presell_pay'] = 1;
                }else{
                    $order_list[$k]['can_presell_pay'] = 0;
                    $order_list[$k]['can_presell_pay_reason'] = '预售订单付尾款时间：'.date('Y-m-d H:i:s',$order_list[$k]['pay_start_time']).'-'.date('Y-m-d H:i:s',$order_list[$k]['pay_end_time']);
                }
            }
            $order_list[$k]['verification_code'] = '';
            $order_list[$k]['verification_qrcode'] = '';
            $order_list[$k]['store_id'] = 0; 
            if($order['store_id']){
                $order_list[$k]['verification_code'] = $order['verification_code'];
                $order_list[$k]['verification_qrcode'] = __IMG($order['verification_qrcode']);
                $order_list[$k]['store_id'] = $order['store_id'];
            }
            //货到付款订单，在未确认收货情况下不能申请退款退货
            if($order['payment_type']==4 && ($order['order_status']<3 || $order['order_status']>4) && $order['order_status'] != -1){
                $order_list[$k]['unrefund'] = 1;
                $order_list[$k]['unrefund_reason'] = '货到付款订单，在未确认收货情况下不能申请退款退货！';
            }
            if($order['order_type']==2 || $order['order_type']==3 || $order['order_type']==4){
                $order_list[$k]['unrefund'] = 1;
                $order_list[$k]['unrefund_reason'] = '成为微店店主、店主等级续费和升级不能售后！';
            }
            if($order['payment_type']==16 || $order['payment_type']==17){
                $order_list[$k]['promotion_status'] = true;
            }
        }
        return json([
            'code' => 1,
            'message' => '获取成功',
            'data' => [
                'order_list' => $order_list,
                'page_count' => $list['page_count'],
                'total_count' => $list['total_count']
            ]
        ]);
    }

    /**
     * 订单列表支付接口
     * 创建新的out_trade_no
     */
    public function orderPay()
    {
        $order_mdl = new VslOrderModel();
        $order_id = request()->post('order_id');
        $order_type_list = $order_mdl->getInfo(['order_id' => $order_id], 'order_type, money_type, out_trade_no, out_trade_no_presell, presell_id,website_id,shop_id');
        if($order_type_list['order_type'] == 7 && !$this->is_presell){
            $data['code'] = 0;
            $data['message'] = '预售已关闭';
            return json($data);
        }
        if($order_type_list['order_type'] == 5){
            if(!$this->groupshopping){
                $data['code'] = 0;
                $data['message'] = '拼团已关闭';
                return json($data);
            }else{
                $group_server = new GroupShopping();
                $checkGroup = $group_server->checkGroupIsCanByOrder($order_type_list['out_trade_no']);
                if($checkGroup < 0){
                    return json(AjaxReturn($checkGroup));
                }
            }
        }
        if (empty($order_id)) {
            return json(AjaxReturn(LACK_OF_PARAMETER));
        }
        $service_order = new OrderService();
        $pay = new UnifyPay();
        if($order_type_list['order_type'] == 7 && $this->is_presell){//预售订单
            if($order_type_list['money_type'] == 0){//待付定金
                $pay_value = $pay->getPresellPayInfo($order_type_list['out_trade_no']);
                $new_out_trade_no = $order_type_list['out_trade_no'];
            }elseif($order_type_list['money_type'] == 1){//待付尾款
                $pay_value = $pay->getPresellPayInfo($order_type_list['out_trade_no_presell']);
                $new_out_trade_no = $order_type_list['out_trade_no_presell'];
            }elseif($order_type_list['money_type'] == 2){
                $data['code'] = 2;
                $data['message'] = '订单已经支付!';
                return json($data);
            }
        }else{
            $new_out_trade_no = $service_order->getOrderNewOutTradeNo($order_id);
            $pay_value = $pay->getPayInfo($new_out_trade_no);
        }
        $config_service = new \data\service\Config();
        $shop_config = $config_service->getShopConfig(0);

        $order_status = $service_order->getOrderStatusByOutTradeNo($new_out_trade_no);
        if (empty($pay_value)) {
            $data['code'] = 0;
            $data['message'] = '订单主体信息已发生变动';
            return json($data);
        }

        if ($pay_value['pay_status'] == 1) {
            // 订单已经支付
            $data['code'] = 2;
            $data['message'] = '订单已经支付或者订单价格为0.00，无需再次支付!';
            return json($data);
        }

        // 订单关闭状态下是不能继续支付的
        if ($order_status == 5) {
            $data['code'] = 0;
            $data['message'] = '订单已关闭，无需再次支付!';
            return json($data);
        }
        if($order_type_list['order_type'] == 7 && $order_type_list['money_type'] == 1){//如果订单类型为预售并且为付尾款状态
            $presell_mdl = new VslPresellModel();
            $pay_end_time = $presell_mdl->getInfo(['id' => $order_type_list['presell_id']], 'pay_end_time')['pay_end_time'];
            if(time() > $pay_end_time){
                $data['code'] = 0;
                $data['message'] = '预售订单支付时间已过期';
                return json($data);
            }
        }else{
            $zero1 = time(); // 当前时间 ,注意H 是24小时 h是12小时
            $zero2 = $pay_value['create_time'];
            if ($zero1 > ($zero2 + ($shop_config['order_buy_close_time'] * 60))) {
                $data['code'] = 0;
                $data['message'] = '支付时间已过期！';
                return json($data);
            }
        }
        $data['code'] = 1;
        $data['message'] = '选择支付方式';
        $data['data']['pay_money'] = $pay_value['pay_money'];
        $data['data']['end_time'] = $zero2 + ($shop_config['order_buy_close_time'] * 60);
        $data['data']['out_trade_no'] = $new_out_trade_no;
        $user_model = new UserModel();
        $user_info = $user_model::get(['user_model.uid' => $this->uid], ['member_account']);
        $data['data']['balance'] = $user_info->member_account->balance ?: 0;
        $data['data']['pay_password'] = !empty($user_info->payment_password) ? 1 : 0;
        $blockchain = getAddons('blockchain',$this->website_id);
        if($blockchain){
            $block = new Block();
            $blockchain_info = $block->getUidInfo($this->uid,$data['data']['pay_money'] );
            $eth_balance = $blockchain_info['eth_balance'];
            $eos_balance = $blockchain_info['eos_balance'];
            $eos_money = $blockchain_info['eos_money'];
            $eth_money = $blockchain_info['eth_money'];
            $eth_paymoney = $blockchain_info['eth_paymoney'];
            $eos_paymoney = $blockchain_info['eos_paymoney'];
        }else{
            $eos_money = '';
            $eth_money = '';
            $eos_balance = '';
            $eth_balance = '';
            $eth_paymoney = '';
            $eos_paymoney = '';
        }
        $data['data']['eth_balance'] =$eth_balance;
        $data['data']['eth_money'] =$eth_money;
        $data['data']['eos_balance'] =$eos_balance;
        $data['data']['eos_money'] =$eos_money;
        $data['data']['eth_paymoney'] =$eth_paymoney;
        $data['data']['eos_paymoney'] =$eos_paymoney;
        return json($data);

    }
    /**
     * 渠道商订单支付接口
     * 创建新的out_trade_no
     */
    public function channelOrderPay()
    {
        $order_id = request()->post('order_id');
        if (empty($order_id)) {
            return json(AjaxReturn(LACK_OF_PARAMETER));
        }
        $service_order = new OrderService();
        $new_out_trade_no = $service_order->getChannelOrderNewOutTradeNo($order_id);

        $pay = new UnifyPay();
        $pay_value = $pay->getChannelPayInfo($new_out_trade_no);
        //获取渠道商设置时间
        $addons_config_mdl = new AddonsConfigModel();
        $channel_setting_value = $addons_config_mdl->getInfo(['addons'=>'channel','website_id'=>$this->website_id], 'value')['value'];
        $channel_setting_arr = json_decode($channel_setting_value,true);
        $order_buy_close_time = $channel_setting_arr['channel_order_close_time']*60;
        $order_status = $service_order->getOrderStatusByOutTradeNo($new_out_trade_no);
        if (empty($pay_value)) {
            $data['code'] = 0;
            $data['message'] = '订单主体信息已发生变动';
            return json($data);
        }
        if ($pay_value['pay_status'] == 1) {
            // 订单已经支付
            $data['code'] = 2;
            $data['message'] = '订单已经支付或者订单价格为0.00，无需再次支付!';
            return json($data);
        }

        // 订单关闭状态下是不能继续支付的
        if ($order_status == 5) {
            $data['code'] = 0;
            $data['message'] = '订单已关闭，无需再次支付!';
            return json($data);
        }

        $zero1 = time(); // 当前时间 ,注意H 是24小时 h是12小时
        $zero2 = $pay_value['create_time'];
        if ($zero1 > ($zero2 + $order_buy_close_time)) {
            $data['code'] = 0;
            $data['message'] = '订单已关闭';
            return json($data);
        } else {
            $data['code'] = 1;
            $data['message'] = '选择支付方式';
            $data['data']['pay_money'] = $pay_value['pay_money'];
            $data['data']['end_time'] = $zero2 + $order_buy_close_time;
            $data['data']['out_trade_no'] = $new_out_trade_no;

            $user_model = new UserModel();
            $user_info = $user_model::get(['user_model.uid' => $this->uid], ['member_account']);
            $data['data']['balance'] = $user_info->member_account->balance ?: 0;
            $data['data']['pay_password'] = !empty($user_info->payment_password) ? 1 : 0;
            return json($data);
        }

    }
    /**
     * 订单详情
     */
    public function orderDetail()
    {
        $order_id = request()->post('order_id');
        if (empty($order_id)) {
            return json(AjaxReturn(LACK_OF_PARAMETER));
        }
        $district_model = new DistrictModel();
        $order_service = new OrderService();
        $order_info = $order_service->getOrderDetail($order_id);
        if($order_info['payment_type'] && $order_info['payment_type_presell']){
            $order_info['payment_type_name'] = OrderStatus::getPayType($order_info['payment_type']) .'+'. OrderStatus::getPayType($order_info['payment_type_presell']);
        }
        if($order_info['payment_type']==16 || $order_info['payment_type']==17 || $order_info['payment_type_presell']==16 || $order_info['payment_type_presell']==17){
            $block = new VslBlockChainRecordsModel();
            $block_info = $block->getInfo(['data_id'=>$order_info['out_trade_no']],'*');
            $block_info1 = $block->getInfo(['data_id'=>$order_info['out_trade_no_presell']],'*');
            if($block_info['from_type']==4 && $block_info1['from_type']==4){
                $order_info['first_real_money'] = floatval($block_info['cash']).'ETH';
                $order_info['final_real_money'] = $block_info1['cash'].'ETH';
                $order_info['order_real_money'] = floatval($block_info['cash']).'ETH +' .$block_info1['cash'].'ETH';
            }
            if($block_info['from_type']==8 && $block_info1['from_type']==4){
                $order_info['first_real_money'] = floatval($block_info['cash']).'EOS';
                $order_info['final_real_money'] = $block_info1['cash'].'ETH';
                $order_info['order_real_money'] = floatval($block_info['cash']).'EOS +' .$block_info1['cash'].'ETH';
            }
            if($block_info['from_type']==4 && $block_info1['from_type']==8){
                $order_info['first_real_money'] = floatval($block_info['cash']).'ETH';
                $order_info['final_real_money'] = $block_info1['cash'].'EOS';
                $order_info['order_real_money'] = floatval($block_info['cash']).'ETH +' .$block_info1['cash'].'EOS';
            }
            if($block_info['from_type']==8 && $block_info1['from_type']==8){
                $order_info['first_real_money'] = floatval($block_info['cash']).'EOS';
                $order_info['final_real_money'] = $block_info1['cash'].'EOS';
                $order_info['order_real_money'] = floatval($block_info['cash']).'EOS +' .$block_info1['cash'].'EOS';
            }
            if($block_info['from_type']==4 && $block_info1['from_type']!=8 && $block_info1['from_type']!=4){
                if($order_info['presell_id'] && $order_info['final_money']){
                    $order_info['first_real_money'] = floatval($block_info['cash']).'ETH';
                    $order_info['final_real_money'] = '¥ '.$order_info['final_money'];
                    $order_info['order_real_money'] = floatval($block_info['cash']).'ETH + ¥ '.$order_info['final_money'];
                }else{
                    $order_info['order_real_money'] = $order_info['coin'].'ETH';
                }
            }
            if($block_info['from_type']!=8 && $block_info['from_type']!=4 && $block_info1['from_type']==8){
                if($order_info['presell_id'] && $order_info['first_money']){
                    $order_info['final_real_money'] = floatval($block_info['cash']).'EOS';
                    $order_info['first_real_money'] = '¥ '.$order_info['first_money'];
                    $order_info['order_real_money'] = '¥ '.$order_info['first_money'].'+' .floatval($block_info['cash']).'EOS';
                }
            }
            if($block_info['from_type']==8 && $block_info1['from_type']!=8 && $block_info1['from_type']!=4){
                if($order_info['presell_id'] && $order_info['final_money']){
                    $order_info['first_real_money'] = floatval($block_info['cash']).'EOS';
                    $order_info['final_real_money'] = '¥ '.$order_info['final_money'];
                    $order_info['order_real_money'] = floatval($block_info['cash']).'EOS + ¥ '.$order_info['final_money'];
                }else{
                    $order_info['order_real_money'] = $order_info['coin'].'EOS';
                }
            }
            if($block_info['from_type']!=4 && $block_info['from_type']!=8 && $block_info1['from_type']==4){
                if($order_info['presell_id'] && $order_info['first_money']){
                    $order_info['final_real_money'] = floatval($block_info['cash']).'ETH';
                    $order_info['first_real_money'] = '¥ '.$order_info['first_money'];
                    $order_info['order_real_money'] = '¥ '.$order_info['first_money'] .'+'.floatval($block_info['cash']).'EOS';
                }
            }
            $order_info['first_money'] = '¥ '.$order_info['first_money'];
            $order_info['final_money'] = '¥ '.$order_info['final_money'];
        }else{
            if($order_info['first_money']!=null){
                $order_info['first_real_money'] = '¥ '.$order_info['first_money'];
            }else{
                $order_info['first_real_money'] = '';
            }
            if($order_info['final_money']!=null){
                $order_info['final_real_money'] = '¥ '.$order_info['final_money'];
            }else{
                $order_info['final_real_money'] = '';
            }
            $order_info['order_real_money'] = '¥ '.$order_info['order_money'];
        }
        $order_detail['order_id'] = $order_info['order_id'];
        $order_detail['order_no'] = $order_info['order_no'];
        $order_detail['shop_name'] = $order_info['shop_name'];
        $order_detail['shop_id'] = $order_info['shop_id'];
        $order_detail['order_status'] = $order_info['order_status'];
        $order_detail['offline_pay'] = $order_info['offline_pay'];
        $order_detail['payment_type_name'] = $order_info['payment_type_name'];
        $order_detail['payment_type'] = $order_info['payment_type'];
        $order_detail['promotion_status'] = ($order_info['promotion_money'] + $order_info['coupon_money'] > 0) ?: false;
        $order_detail['order_refund_status'] = reset($order_info['order_goods'])['refund_status'];
        $order_detail['is_evaluate'] = $order_info['is_evaluate'];
        $order_detail['first_money'] = $order_info['first_money'];
        $order_detail['final_money'] = $order_info['final_money'];
        $order_detail['first_real_money'] = $order_info['first_real_money'];
        $order_detail['final_real_money'] = $order_info['final_real_money'];
        $order_detail['order_money'] = $order_info['order_money'];
        $order_detail['order_real_money'] = $order_info['order_real_money'];
        $order_detail['goods_money'] = $order_info['goods_money'];
        $order_detail['shipping_fee'] = $order_info['shipping_money'] - $order_info['promotion_free_shipping'];
        $order_detail['promotion_money'] = 0;
        //订单时间信息
        $order_detail['create_time'] = $order_info['create_time'];
        $order_detail['pay_time'] = $order_info['pay_time'];
        $order_detail['consign_time'] = $order_info['consign_time'];
        $order_detail['finish_time'] = $order_info['finish_time'];
        $address_info = $district_model::get($order_info['receiver_district'], ['city.province']);
        $order_detail['receiver_name'] = $order_info['receiver_name'];
        $order_detail['receiver_mobile'] = $order_info['receiver_mobile'];
        $order_detail['receiver_province'] = $address_info->city->province->province_name;
        $order_detail['receiver_city'] = $address_info->city->city_name;
        $order_detail['receiver_district'] = $address_info->district_name;
        $order_detail['receiver_address'] = $order_info['receiver_address'];
        $order_detail['buyer_message'] = $order_info['buyer_message'];
        $order_detail['group_id'] = $order_info['group_id'];
        $order_detail['group_record_id'] = $order_info['group_record_id'];
        $order_detail['store_id'] = 0;
        $order_detail['card_store_id'] = $order_info['card_store_id'];
        $order_detail['verification_code'] = '';
        $order_detail['verification_qrcode'] = '';
        $order_detail['presell_status'] = $order_info['presell_status'];
        $order_detail['order_type'] = $order_info['order_type'];
        $order_detail['deduction_money'] = $order_info['deduction_money'];
        //积分兑换订单
        $order_detail['order_point'] = $order_info['point'];
        $order_detail['can_presell_pay'] = $order_info['can_presell_pay'];
        $order_detail['can_presell_pay_reason'] = $order_info['can_presell_pay_reason'];
        if($order_info['store_id']){
            $order_detail['store_id'] = $order_info['store_id'];
            $order_detail['verification_code'] = $order_info['verification_code'];
            $order_detail['verification_qrcode'] = __IMG($order_info['verification_qrcode']);
            $order_detail['store_name'] = $order_info['order_pickup']['store_name'];
            $order_detail['store_tel'] = $order_info['order_pickup']['store_tel'];
            $order_detail['receiver_province'] = $order_info['order_pickup']['province_name'];
            $order_detail['receiver_city'] = $order_info['order_pickup']['city_name'];
            $order_detail['receiver_district'] = $order_info['order_pickup']['dictrict_name'];
            $order_detail['receiver_address'] = $order_info['order_pickup']['address'];
        }
        if ($order_info['payment_type'] == 6 || $order_info['shipping_type'] == 2) {
            $order_status_info = OrderService\OrderStatus::getSinceOrderStatus()[$order_info['order_status']];
        } else {
            $order_status_info = OrderService\OrderStatus::getOrderCommonStatus(1,0,$order_info['card_store_id'],$order_info['order_goods'] ? $order_info['order_goods'][0]['goods_type'] : 0)[$order_info['order_status']];
        }
        $order_detail['is_virtual'] = 0;//是否是积分虚拟商品，用于去掉订单详情物流信息
        //积分订单 虚拟商品是没有物流信息的
        if($order_info['order_type'] == 10){
            if($order_info['order_goods'][0]['goods_exchange_type'] != 0){//都为虚拟商品
                $order_detail['is_virtual'] = 1;
                foreach($order_status_info['member_operation'] as $m_k =>$m_v){
                    if($m_v['no'] == 'logistics'){
                        unset($order_status_info['member_operation'][$m_k]);
                    }
                    if($m_v['no'] == 'evaluation'){
                        unset($order_status_info['member_operation'][$m_k]);
                    }
                    if($m_v['no'] == 'buy_again'){
                        unset($order_status_info['member_operation'][$m_k]);
                    }
                }
                $order_status_info['member_operation'] = array_values($order_status_info['member_operation']);
            }
        }
        if($order_info['money_type']==1 && $order_info['order_type']== 7 && $order_info['order_status']!=5){
            $order_status_info['member_operation'] = array(['no' => 'last_money','name' => '付尾款',  'icon_class' => 'icon icon-pay-l']   );
        }elseif($order_info['money_type']==2 && $order_info['order_type']== 7 && $order_info['order_status']!=5){
        }elseif($order_info['order_status'] == 1 && $order_info['goods_packet_list']){
            $order_status_info['member_operation'] = array(['no' => 'logistics','name' => '查看物流','icon_class'=>'icon icon-preview-l',]);
        }
        $order_detail['member_operation'] = $order_status_info['member_operation'];
        $order_detail['no_delivery_id_array'] = [];
        foreach ($order_info['order_goods_no_delive'] as $v_goods) {
            $order_detail['no_delivery_id_array'][] = $v_goods['order_goods_id'];
        }

        $goods_packet_list = [];
        foreach ($order_info['goods_packet_list'] as $k => $v_packet) {
            $goods_packet_list[$k]['packet_name'] = $v_packet['packet_name'];
//            $goods_packet_list[$k]['express_name'] = $v_packet['express_name'];
//            $goods_packet_list[$k]['express_code'] = $v_packet['express_code'];
            $goods_packet_list[$k]['shipping_info'] = $v_packet['shipping_info'];
            $goods_packet_list[$k]['order_goods_id_array'] = [];
            foreach ($v_packet['order_goods_list'] as $k_o => $v_goods) {
                $goods_packet_list[$k]['order_goods_id_array'][] = $v_goods['order_goods_id'];
            }
        }
        $order_detail['goods_packet_list'] = $goods_packet_list;
        $order_detail['goods_type'] = 0;
        if($order_info['order_goods']){
            $order_detail['goods_type'] = $order_info['order_goods'][0]['goods_type'];
        }
        $order_goods = [];
        foreach ($order_info['order_goods'] as $k => $v) {
            $order_goods[$k]['order_goods_id'] = $v['order_goods_id'];
            $order_goods[$k]['goods_id'] = $v['goods_id'];
            $order_goods[$k]['goods_name'] = $v['goods_name'];
            $order_goods[$k]['sku_id'] = $v['sku_id'];
            $order_goods[$k]['sku_name'] = $v['sku_name'];
            $order_goods[$k]['price'] = $v['price'];
            $order_goods[$k]['goods_point'] = $v['goods_point'];
            $order_goods[$k]['num'] = $v['num'];
            $order_goods[$k]['refund_status'] = $v['refund_status'];
            $order_goods[$k]['spec'] = $v['spec'];
            $order_goods[$k]['pic_cover'] = $v['picture_info']['pic_cover'] ? getApiSrc($v['picture_info']['pic_cover']) : '';
            $order_goods[$k]['member_operation'] = [];
            if($order_info['payment_type']==16 || $order_info['payment_type']==17 || $order_info['payment_type_presell']==16 || $order_info['payment_type_presell']==17){
                $order_detail['promotion_status'] = true;
            }
            $order_detail['promotion_money'] += round(($v['price'] - $v['actual_price']) * $v['num'], 2) + $v['promotion_free_shipping'];
            //积分兑换订单是没有售后的
            if ( !in_array($order_info['order_type'], [2, 3, 4, 10]) && !in_array($order_info['order_status'], [4, 5])) {
                if ($v['refund_status'] != 0) {
                    $refund_info = OrderService\OrderStatus::getRefundStatus()[$v['refund_status']];
                    if (!$order_detail['promotion_status']) {
                        $order_goods[$k]['member_operation'] = $refund_info['member_operation'];
                    } else {
                        $temp_member_refund_operation = $refund_info['member_operation'];
                    }
                } else {
                    if (!$order_detail['promotion_status'] && is_array($order_status_info['refund_member_operation'])) {
                        $order_goods[$k]['member_operation'] = array_merge($order_goods[$k]['member_operation'], $order_status_info['refund_member_operation']);
                    } else {
                        $temp_member_refund_operation = $order_status_info['refund_member_operation'];
                    }
                }
            }
        }
        if (!empty($temp_member_refund_operation)) {
            $order_detail['member_operation'] = array_merge($order_detail['member_operation'], $temp_member_refund_operation);
        }
        if($order_info['order_status']==6){
            $order_detail['member_operation'] = [];
            $order_detail['status_name'] = '链上处理中';
        }
        $order_detail['order_goods'] = $order_goods;
        $order_detail['unrefund'] = $order_info['unrefund'];
        $order_detail['unrefund_reason'] = $order_info['unrefund_reason'];
        return json(['code' => 1,
            'message' => '获取成功',
            'data' => $order_detail
        ]);
    }

    public function orderShippingInfo()
    {
        $order_id = request()->post('order_id');
        if (empty($order_id)) {
            return json(AjaxReturn(LACK_OF_PARAMETER));
        }

        $order_service = new OrderService();
        $express_company_model = new VslOrderExpressCompanyModel();
        $order_info = $order_service->getOrderDetail($order_id);
        $order_detail['order_no'] = $order_info['order_no'];

        $order_detail['no_delivery_id_array'] = [];
        foreach ($order_info['order_goods_no_delive'] as $v_goods) {
            $order_detail['no_delivery_id_array'][] = $v_goods['order_goods_id'];
        }

        $goods_packet_list = [];
        foreach ($order_info['goods_packet_list'] as $k => $v_packet) {
            $goods_packet_list[$k]['packet_name'] = $v_packet['packet_name'];
//            $goods_packet_list[$k]['express_name'] = $v_packet['express_name'];
//            $goods_packet_list[$k]['express_code'] = $v_packet['express_code'];
            $express_company_info = $express_company_model::get(['company_name' => $v_packet['shipping_info']['expTextName']]);
            $goods_packet_list[$k]['express_company_logo'] = $express_company_info['express_logo'];
            $goods_packet_list[$k]['shipping_info'] = $v_packet['shipping_info'] ?: ['mailNo'=>$v_packet['express_code'],'expTextName'=>$v_packet['express_name']];
            $goods_packet_list[$k]['order_goods_id_array'] = [];
            foreach ($v_packet['order_goods_list'] as $k_o => $v_goods) {
                $goods_packet_list[$k]['order_goods_id_array'][] = $v_goods['order_goods_id'];
            }
        }
        $order_detail['goods_packet_list'] = $goods_packet_list;

        $order_goods = [];
        foreach ($order_info['order_goods'] as $k => $v) {
            $order_goods[$k]['order_goods_id'] = $v['order_goods_id'];
            $order_goods[$k]['goods_id'] = $v['goods_id'];
            $order_goods[$k]['goods_name'] = $v['goods_name'];
            $order_goods[$k]['sku_id'] = $v['sku_id'];
            $order_goods[$k]['sku_name'] = $v['sku_name'];
            $order_goods[$k]['price'] = $v['price'];
            $order_goods[$k]['num'] = $v['num'];
            $order_goods[$k]['spec'] = $v['spec'];
            $order_goods[$k]['pic_cover'] = $v['picture_info']['pic_cover'] ? getApiSrc($v['picture_info']['pic_cover']) : '';
        }

        $order_detail['order_goods'] = $order_goods;

        return json(['code' => 1,
            'message' => '获取成功',
            'data' => $order_detail
        ]);
    }

    /**
     * 订单评价
     */
    public function addOrderEvaluate()
    {
        $order = new OrderService();
        $order_id = request()->post('order_id');
        $goods_evaluate = request()->post('goods_evaluate/a');
        $shop_desc = request()->post('shop_desc');
        $shop_service = request()->post('shop_service');
        $store_service = request()->post('store_service');
        $shop_stic = request()->post('shop_stic');
        if (empty($order_id) || empty($goods_evaluate)) {
            return json(AjaxReturn(LACK_OF_PARAMETER));
        }
        $user_model = new UserModel();
        $order_model = new VslOrderModel();
        $order_info = $order_model::get($order_id);
        $user_info = $user_model::get($this->uid);
        if ($shop_desc || $shop_service || $shop_stic) {
            if($this->is_shop){
                $data_shop = array(
                    'order_id' => $order_id,
                    'order_no' => $order_info['order_no'],
                    'website_id' => $this->website_id,
                    'shop_id' => $order_info['shop_id'],
                    'shop_desc' => ($shop_desc > 5 || $shop_desc < 0) ? 5 : $shop_desc,
                    'shop_service' => ($shop_service > 5 || $shop_service < 0) ? 5 : $shop_service,
                    'shop_stic' => ($shop_stic > 5 || $shop_stic < 0) ? 5 : $shop_stic,
                    'add_time' => time(),
                    'member_name' => $user_info['user_name'],
                    'uid' => $this->uid,
                );
                $order->addShopEvaluate($data_shop);
            }
            
        }
        if(getAddons('store', $this->website_id, $order_info['shop_id']) && $order_info['store_id']){
            $storeServer = new Store();
            $data_store = array(
                'order_id' => $order_id,
                'order_no' => $order_info['order_no'],
                'website_id' => $this->website_id,
                'shop_id' => $order_info['shop_id'],
                'store_id' => $order_info['store_id'],
                'store_service' => ($store_service > 5 || $store_service < 0) ? 5 : $store_service,
                'add_time' => time(),
                'member_name' => $user_info['user_name'],
                'uid' => $this->uid,
            );
            $storeServer->addStoreEvaluate($data_store);
        }
        $dataArr = array();
        foreach ($goods_evaluate as $key => $evaluate) {
            $orderGoods = $order->getOrderGoodsInfo($evaluate['order_goods_id']);
            $data = array(
                'order_id' => $order_id,
                'order_no' => $order_info['order_no'],
                'order_goods_id' => $evaluate['order_goods_id'],
                'website_id' => $orderGoods['website_id'],
                'goods_id' => $orderGoods['goods_id'],
                'goods_name' => $orderGoods['goods_name'],
                'goods_price' => $orderGoods['goods_money'],
                'goods_image' => $orderGoods['goods_picture'],
                'shop_id' => $orderGoods['shop_id'],
                'content' => $evaluate['content'],
                'addtime' => time(),
                'image' => (!empty($evaluate['images']) && is_array($evaluate['images'])) ? implode(",", $evaluate['images']) : '',
                'member_name' => $user_info['user_name'],
                'explain_type' => ($evaluate['explain_type'] > 5 || $evaluate['explain_type'] < 0) ? 1 : $evaluate['explain_type'],
                'uid' => $this->uid,
            );
            $dataArr[] = $data;
        }
        //var_dump($dataArr);exit;
        $result = $order->addGoodsEvaluate($dataArr, $order_id);
        if ($result) {
            return json(['code' => 1, 'message' => '成功评价']);
        } else {
            return json(['code' => -1, 'message' => '评价失败']);
        }
    }

    /**
     * 点单追评
     */
    public function addOrderEvaluateAgain()
    {
        $order = new OrderService();
        $order_id = request()->post('order_id');
        $goods_evaluate = request()->post('goods_evaluate/a');
        if (empty($order_id) || empty($goods_evaluate)) {
            return json(AjaxReturn(LACK_OF_PARAMETER));
        }

        $result = true;
        foreach ($goods_evaluate as $key => $evaluate) {
            $images = (!empty($evaluate['images']) && is_array($evaluate['images'])) ? implode(",", $evaluate['images']) : '';

            $res = $order->addGoodsEvaluateAgain($evaluate['content'], $images, $evaluate['order_goods_id']);
            if ($res == false) {

                $result = false;
                break;
            }
        }
        if ($result == 1) {
            $data = array(
                'is_evaluate' => 2
            );
            $result = $order->modifyOrderInfo($data, $order_id);
        }
        if ($result) {
            return json(['code' => 1, 'message' => '成功评价']);
        } else {
            return json(['code' => -1, 'message' => '评价失败']);
        }
    }

    /**
     * 删除订单
     */
    public function deleteOrder()
    {
        $order_service = new OrderService();
        $order_id = request()->post('order_id');
        if (empty($order_id)) {
            return json(AjaxReturn(LACK_OF_PARAMETER));
        }
        $res = $order_service->deleteOrder($order_id, 2, $this->uid);
        if ($res) {
            return json(['code' => 1, 'message' => '删除成功']);
        } else {
            return json(['code' => -1, 'message' => '删除失败']);
        }
    }

    /**
     * 收货
     */
    public function orderTakeDelivery()
    {
        $order_service = new OrderService();
        $order_id = request()->post('order_id');
        if (empty($order_id)) {
            return json(AjaxReturn(LACK_OF_PARAMETER));
        }
        $res = $order_service->OrderTakeDelivery($order_id);
        if ($res) {
            return json(['code' => 1, 'message' => '成功收货']);
        } else {
            return json(['code' => -1, 'message' => '收货失败']);
        }
    }

    /**
     * 交易关闭
     */
    public function orderClose()
    {
        $form_id = request()->post('form_id', '');
        $order_service = new OrderService();
        $order_id = request()->post('order_id');
        if (empty($order_id)) {
            return json(AjaxReturn(LACK_OF_PARAMETER));
        }
        // 小程序关闭订单模板消息
        if ($form_id) {
            $order_service->setOrderPaymentFormIdByOrderId($order_id, $form_id);
        }
        $res = $order_service->orderClose($order_id, 1);
        if ($res) {
            return json(['code' => 1, 'message' => '成功关闭']);
        } else {
            return json(['code' => -1, 'message' => '关闭失败']);
        }
    }

    /**
     * 售后页面
     */
    public function refundDetail()
    {
        $order_goods_id = request()->post('order_goods_id');
        $order_id = request()->post('order_id');
        $presell_id = request()->post('presell_id','');
        if (!is_numeric($order_goods_id) && !is_numeric($order_id)) {
            return json(AjaxReturn(LACK_OF_PARAMETER));
        }
        $order_service = new OrderService();
        if ($order_goods_id) {
            $condition['vsl_order_goods.order_goods_id'] = $order_goods_id;
        }
        $data['is_all'] = 0;
        if ($order_id) {
            $condition['vsl_order_goods.order_id'] = $order_id;
        }
        $condition['vsl_order_goods.buyer_id'] = $this->uid;
        $detail = $order_service->getOrderGoodsRefundInfoNew($condition);
        if (count($detail['goods_list']) == 0) {
            return json(['code' => -1, 'message' => '对不起,您无权进行此操作']);
        }
        $detail['goods_type'] = $detail['goods_list'][0]['goods_type'];
        $order_model = new VslOrderModel();
        $payment_type = $order_model->getInfo(['order_id'=>$detail['order_id']],'*');
        //货到付款订单 退款需要扣除运费
        // if($payment_type['payment_type'] == 4){
        //     $detail['refund_max_money'] -= $payment_type['shipping_money'];
        // }
        foreach ($detail['goods_list'] as &$v) {
            $v['pic_cover'] = getApiSrc($v['pic_cover']);
            //如果是预售的话拿尾款金额，规格ID和预售ID获取信息
            if(!empty($order_model['presell_id'])){
                $presell_id = $order_model['presell_id'];
                $presell = new Presell();
                $rule = $presell->get_presell_by_sku($presell_id,$v['sku_id']);
                if(!empty($rule)){
                    $detail['refund_max_money'] = ($rule['allmoney']-$rule['firstmoney'])*$v['num'];
                }
            }
        }
        $detail['refund_eth_money'] = '';
        $detail['refund_eth_charge'] = '';
        $detail['refund_eos_money'] = '';
        $detail['refund_eth_val'] = '';
        $detail['refund_eos_charge'] = '';
        $detail['refund_eos_val'] = '';
        $detail['eth_status'] = false;
        $detail['eos_status'] = false;
        $money = 0;
        if($payment_type['presell_id']){
            if($payment_type['payment_type']==16 && $payment_type['payment_type_presell']==16) {
                $data['is_all'] = 1;
                $block = new VslBlockChainRecordsModel();
                $blocks = new Block();
                $block_info = $block->getInfo(['data_id' => $payment_type['out_trade_no']], '*');
                $block_info1 = $block->getInfo(['data_id' => $payment_type['out_trade_no_presell']], '*');
                if ($block_info && $block_info['from_type'] == 4 && $block_info1 && $block_info1['from_type'] == 4) {
                    $money = $payment_type['shipping_money'];//运费（预售退尾款时才退手续费）
                    $charge = $blocks->ethRefundMoney($money);//运费不退扣除
                    $real_coin1 = floatval($block_info['cash']) + $block_info1['cash']-$charge;
                    $detail['refund_eth_money'] = $real_coin1 . 'ETH';//退款金额
                    $real_coin2 = floatval($payment_type['coin_charge']) * 4;
                    $detail['refund_eth_charge'] = $real_coin2 . 'ETH';//手续费（预售的支付是两笔手续费，退款又会产生两笔手续费）
                    $real_coin3 = $real_coin1-$real_coin2;
                    $detail['refund_eth_val'] = $real_coin3 . 'ETH';
                    if ($real_coin3 <= 0) {
                        $detail['refund_eth_val'] = '0ETH';
                        $detail['eth_status'] = true;
                        $detail['eos_status'] = true;
                    }
                }
            }
            if($payment_type['payment_type']==16 && $payment_type['payment_type_presell']==17) {
                $data['is_all'] = 1;
                $block = new VslBlockChainRecordsModel();
                $blocks = new Block();
                $block_info = $block->getInfo(['data_id' => $payment_type['out_trade_no']], '*');
                $block_info1 = $block->getInfo(['data_id' => $payment_type['out_trade_no_presell']], '*');
                if($block_info && $block_info['from_type']==4 && $block_info1 && $block_info1['from_type']==8){
                    $detail['refund_eth_money'] = floatval($block_info['cash']).'ETH';
                    $real_coin1 = floatval($payment_type['coin_charge'])*2;
                    $detail['refund_eth_charge'] = $real_coin1.'ETH';
                    $real_coin2= floatval($block_info['cash'])-$real_coin1;
                    $detail['refund_eth_val'] = $real_coin2.'ETH';
                    if($real_coin2<=0){
                        $detail['refund_eth_val'] = '0ETH';
                        $detail['eth_status'] = true;
                    }
                    $money = $payment_type['shipping_money'];//运费（预售退尾款时才退手续费）
                    $charge1 = $blocks->eosRefundMoney($money);
                    $real_coin3 = $block_info1['cash']-$charge1;
                    $detail['refund_eos_money'] = $real_coin3.'EOS';
                    if($real_coin3<=0){
                        $detail['refund_eos_money'] = '0EOS';
                        $detail['eos_status'] = true;
                    }
                }
            }
            if($payment_type['payment_type']==17 && $payment_type['payment_type_presell']==16) {
                $data['is_all'] = 1;
                $block = new VslBlockChainRecordsModel();
                $blocks = new Block();
                $block_info = $block->getInfo(['data_id' => $payment_type['out_trade_no']], '*');
                $block_info1 = $block->getInfo(['data_id' => $payment_type['out_trade_no_presell']], '*');
                if($block_info && $block_info['from_type']==8 && $block_info1 && $block_info1['from_type']==4){
                    $detail['refund_eos_money'] = floatval($block_info['cash']).'EOS';
                    $money = $payment_type['shipping_money'];//运费（预售退尾款时才退手续费）
                    $charge = $blocks->ethRefundMoney($money);
                    $real_coin1 = $block_info1['cash']-$charge;
                    $detail['refund_eth_money'] = $real_coin1.'ETH';
                    $real_coin2 = floatval($payment_type['coin_charge'])*2;
                    $detail['refund_eth_charge'] = $real_coin2.'ETH';
                    $real_coin3 =  $real_coin1-$real_coin2;
                    $detail['refund_eth_val'] = $real_coin3.'ETH';
                    if($real_coin3<=0){
                        $detail['refund_eth_val'] = '0ETH';
                        $detail['eth_status'] = true;
                    }
                }
            }
            if($payment_type['payment_type']==17 && $payment_type['payment_type_presell']==17) {
                $data['is_all'] = 1;
                $block = new VslBlockChainRecordsModel();
                $blocks = new Block();
                $block_info = $block->getInfo(['data_id' => $payment_type['out_trade_no']], '*');
                $block_info1 = $block->getInfo(['data_id' => $payment_type['out_trade_no_presell']], '*');
                if($block_info && $block_info['from_type']==8 && $block_info1 && $block_info1['from_type']==8){
                    $money = $payment_type['shipping_money'];//运费（预售退尾款时才退手续费）
                    $charge = $blocks->eosRefundMoney($money);
                    $real_coin = floatval($block_info['cash'])+$block_info1['cash']-$charge;
                    $detail['refund_eos_money'] = $real_coin.'EOS';
                    if($real_coin<=0){
                        $detail['refund_eos_money'] = '0EOS';
                        $detail['eos_status'] = true;
                        $detail['eth_status'] = true;
                    }
                }
            }
            if($payment_type['payment_type']==16 && $payment_type['payment_type_presell']!=16 && $payment_type['payment_type_presell']!=17) {
                $data['is_all'] = 1;
                $block = new VslBlockChainRecordsModel();
                $block_info = $block->getInfo(['data_id' => $payment_type['out_trade_no']], '*');
                if ($block_info && $block_info['from_type'] == 4) {
                    $detail['refund_eth_money'] = floatval($block_info['cash']) . 'ETH';
                    $detail['refund_eth_charge'] = (floatval($payment_type['coin_charge']) * 2) . 'ETH';
                    $real_coin = floatval($block_info['cash']) - floatval($payment_type['coin_charge'])*2;
                    $detail['refund_eth_val'] = $real_coin . 'ETH';
                    if ($real_coin <= 0) {
                        $detail['refund_eth_val'] = '0ETH';
                        $detail['eth_status'] = true;
                        if($payment_type['final_money']==0){
                            $detail['eos_status'] = true;
                        }
                    }
                }
            }
            if($payment_type['payment_type']==17 && $payment_type['payment_type_presell']!=16 && $payment_type['payment_type_presell']!=17) {
                $data['is_all'] = 1;
                $block = new VslBlockChainRecordsModel();
                $blocks = new Block();
                $block_info = $block->getInfo(['data_id' => $payment_type['out_trade_no']], '*');
                if ($block_info && $block_info['from_type'] == 8) {
                    $money = $payment_type['shipping_money'];
                    $charge = $blocks->eosRefundMoney($money);
                    $real_coin = floatval($block_info['cash'])-$charge;
                    $detail['refund_eos_money'] = $real_coin . 'EOS';
                    if ($real_coin <= 0) {
                        $detail['eos_status'] = true;
                        $detail['refund_eos_money'] = '0EOS';
                        if($payment_type['final_money']==0){
                            $detail['eth_status'] = true;
                        }
                    }
                }
            }
            if($payment_type['payment_type_presell']==16 && $payment_type['payment_type']!=16 && $payment_type['payment_type']!=17) {
                $data['is_all'] = 1;
                $block = new VslBlockChainRecordsModel();
                $blocks = new Block();
                $block_info = $block->getInfo(['data_id' => $payment_type['out_trade_no_presell']], '*');
                if ($block_info && $block_info['from_type'] == 4) {
                    $money = $payment_type['shipping_money'];
                    $charge = $blocks->ethRefundMoney($money);
                    $detail['refund_eth_money'] = floatval($block_info['cash']) . 'ETH';
                    $detail['refund_eth_charge'] = (floatval($payment_type['coin_charge']) * 2) . 'ETH';
                    $real_coin = floatval($block_info['cash']) -$charge- floatval($payment_type['coin_charge'])*2;
                    $detail['refund_eth_val'] = $real_coin . 'ETH';
                    if ($real_coin <= 0) {
                        $detail['refund_eth_val'] = '0ETH';
                        $detail['eth_status'] = true;
                        if($payment_type['pay_money']==0){
                            $detail['eos_status'] = true;
                        }
                    }
                }
            }
            if($payment_type['payment_type_presell']==17 && $payment_type['payment_type']!=16 && $payment_type['payment_type']!=17) {
                $data['is_all'] = 1;
                $block = new VslBlockChainRecordsModel();
                $blocks = new Block();
                $block_info = $block->getInfo(['data_id' => $payment_type['out_trade_no_presell']], '*');
                if ($block_info && $block_info['from_type'] == 8) {
                    $money = $payment_type['shipping_money'];
                    $charge = $blocks->ethRefundMoney($money);
                    $real_coin = floatval($block_info['cash']) -$charge;
                    $detail['refund_eos_money'] = $real_coin . 'EOS';
                    if ($real_coin <= 0) {
                        $detail['refund_eos_money'] = '0EOS';
                        $detail['eos_status'] = true;
                        if($payment_type['pay_money']==0){
                            $detail['eth_status'] = true;
                        }
                    }
                }
            }
        }else{
            if($payment_type['payment_type']==16) {
                $data['is_all'] = 1;
                $block = new VslBlockChainRecordsModel();
                $block_info = $block->getInfo(['data_id' => $payment_type['out_trade_no']], '*');
                if ($block_info && $block_info['from_type'] == 4) {
                    if($payment_type['shipping_money'] && $payment_type['shipping_status']>=1){
                        $money = $payment_type['shipping_money'];
                    }
                    $detail['refund_eth_money'] = (floatval($block_info['cash']) - floatval($block_info['gas']) -floatval($block_info['to_charge'])) . 'ETH';////
                    $detail['refund_eth_charge'] = floatval($payment_type['coin_charge']) . 'ETH'; /////
//                    $real_coin = floatval($block_info['cash'])-floatval($payment_type['coin_charge'])*2;
                    $real_coin = floatval($block_info['cash']) - floatval($block_info['gas']) -floatval($block_info['to_charge'])-floatval($payment_type['coin_charge']);
                    if ($real_coin<= 0) {
                        $detail['refund_eth_val'] = '0ETH';
                        $detail['eth_status'] = true;
                        $detail['eos_status'] = true;
                    }
                }
            }
            if($payment_type['payment_type']==17) {
                $data['is_all'] = 1;
                $block = new VslBlockChainRecordsModel();
                $blocks = new Block();
                $block_info = $block->getInfo(['data_id' => $payment_type['out_trade_no']], '*');
                if ($block_info && $block_info['from_type'] == 8) {
                    if($payment_type['shipping_money'] && $payment_type['shipping_status']>=1){
                        $money = $payment_type['shipping_money'];
                    }
                    $charge = $blocks->eosRefundMoney($money);
                    $real_coin = floatval($block_info['cash'])-$charge;
                    $detail['refund_eos_money'] = $real_coin . 'EOS';
                    if ($real_coin <= 0) {
                        $detail['eth_status'] = true;
                        $detail['eos_status'] = true;
                        $detail['refund_eos_money'] = '0EOS';
                    }
                }
            }
        }
        unset($v);
        $data['refund_detail'] = $detail;
        $data['company_list'] = [];
        if ($detail['refund_type'] == 2) {
            // 物流公司,只显示后台启用的物流公司
            $comShopRela = new VslExpressCompanyShopRelationModel();
            $usedCompany = $comShopRela->Query(['website_id' =>$this->website_id,'shop_id' => $detail['shop_id']], 'co_id');
            if($usedCompany){
                $company = new VslOrderExpressCompanyModel();
                $data['company_list'] = $company->getViewList(1, 0, ['co_id' => ['in',$usedCompany]], '')['data'];
            }
        }
        // 查询商家或者店铺地址
        $shop_info = $order_service->getShopReturn($detail['return_id'],$detail['shop_id'], $this->website_id);
        $address = new Address();
        $province_name = $address->getProvinceName($shop_info['province']);
        $city_name = $address->getCityName($shop_info['city']);
        $dictrict_name = $address->getDistrictName($shop_info['district']);
        $shop_info['address'] = $province_name.$city_name.$dictrict_name.$shop_info['address'];
        unset($shop_info['create_time'], $shop_info['modify_time'], $shop_info['website_id'], $shop_info['city'], $shop_info['district'], $shop_info['province'], $shop_info['is_default']);
        $data['shop_info'] = $shop_info;

        return json(
            [
                'code' => 1,
                'message' => '成功获取',
                'data' => $data
            ]
        );
    }

    /**
     * 取消退款
     */
    public function cancelOrderRefund()
    {
        $orderService = new OrderService();
        $order_id = request()->post('order_id', '');
        $order_goods_id = request()->post('order_goods_id/a');
        if (empty($order_id) || empty($order_goods_id)) {
            return json(AjaxReturn(LACK_OF_PARAMETER));
        }
        $cancel_order = $orderService->orderGoodsCancel($order_id, $order_goods_id);
        return json($cancel_order);
    }

    /**
     * 退款/退货申请提交
     */
    public function refundAsk()
    {
        $order_id = request()->post('order_id');
        $order_goods_id = request()->post('order_goods_id/a');
        $refund_type = request()->post('refund_type');
        $refund_require_money = request()->post('refund_require_money');
        $refund_reason = request()->post('refund_reason');
        $form_id = request()->post('form_id', '');
        if (empty($order_id) || empty($order_goods_id) || empty($refund_type) || empty($refund_reason)) {
            return json(AjaxReturn(LACK_OF_PARAMETER));
        }
        if($refund_require_money<=0){
            return json(['code'=>-1,'message'=>'退款金额必须大于0']);
        }
        $order_service = new OrderService();
        $retval = $order_service->orderGoodsRefundAskfor($order_id, $order_goods_id, $refund_type, $refund_require_money, $refund_reason,$this->uid, $form_id);
        return json($retval);
    }

    /**
     * 买家退货
     *
     * @return Ambigous <multitype:unknown, multitype:unknown unknown string >
     */
    public function orderGoodsRefundExpress()
    {
        $order_id = request()->post('order_id');
        $order_goods_id = request()->post('order_goods_id/a');
        $refund_express_company = request()->post('refund_express_company');
        $refund_shipping_no = request()->post('refund_shipping_no');
        if (empty($order_id) || empty($order_goods_id) || empty($refund_shipping_no)) {
            return json(AjaxReturn(LACK_OF_PARAMETER));
        }
        $order_service = new OrderService();
        $retval = $order_service->orderGoodsReturnGoods($order_id, $order_goods_id, $refund_express_company, $refund_shipping_no);
        if ($retval > 0) {
            return json(AjaxReturn($retval));
        } else {
            return json(AjaxReturn(SYSTEM_ERROR));
        }
    }

    /**
     *
     * 创建订单
     */
    public function orderCreate()
    {
        $order_service = new OrderService();
        $order_business = new OrderBusiness();
        $user_model = new UserModel();
        $post_data = request()->post('order_data/a');
        //test 
        $calculate_result = $order_service->calculateCreateOrderData($post_data);
       
        $is_order_key = md5(json_encode($post_data));
        //判断是否是网络延迟造成多次请求
        if(Cookie::get($is_order_key)){//有session说明创建成功了
            $cookie_data = unserialize(Cookie::get($is_order_key));
            $time = $cookie_data['create_time'] + 5;
            if(time() <= $time){
                $message['code'] = 0;
                $message['message'] = "订单提交成功";
                $message['data']['out_trade_no'] = $cookie_data['out_trade_no'];
                Cookie::delete($is_order_key);
                return json($message);
            }
        }
        
        $calculate_result = $order_service->calculateCreateOrderData($post_data);
        if ($calculate_result['result'] === false) {
            return json(['code' => -1, 'message' => $calculate_result['message']]);
        }
        if ($calculate_result['result'] === -2) {
            return json(['code' => -2, 'message' => $calculate_result['message']]);
        }
        $order_data = $calculate_result['data'];
        // 订单来源,1 微信浏览器,4 ios,5 Android,6 小程序,2 手机浏览器,3 PC
        $order_from = $post_data['order_from'] ?: 2;
        //return $order_data;
        $uid = $this->uid;
        $ip = get_ip();
        //判断传过来的商品信息是否有秒杀状态商品
        $order_type = $this->isSeckillOrder($order_data['order']);
        //如果是秒杀订单则加入秒杀订单队列
        if ($order_type) {
            $sec_service = new seckillServer();
            $redis = $this->connectRedis();
            foreach ($order_data['order'] as $shop_id => $v) {
                foreach ($v['sku'] as $sku => $sku_list) {
                    $goods_id = $sku_list['goods_id'];
                    $sku_id = $sku_list['sku_id'];
                    $num = $sku_list['num'];
                    $goods_name = $sku_list['goods_name'];
                    $seckill_id = $sku_list['seckill_id'];
                    $condition_is_seckill['s.seckill_id'] = $seckill_id;
                    $condition_is_seckill['nsg.sku_id'] = $sku_id;
                    $is_seckill = $sec_service->isSeckillGoods($condition_is_seckill);
                    //判断活动库存
                    $condition_seckill_sku['seckill_id'] = $seckill_id;
                    $condition_seckill_sku['sku_id'] = $sku_id;
                    if ($is_seckill) {
                        $redis_goods_sku_store_key = 'store_' . $seckill_id . '_' . $goods_id . '_' . $sku_id;
                        for ($i = 0; $i < $num; $i++) {
                            $is_store = $redis->lpop($redis_goods_sku_store_key);
                        }
                        if (!$is_store) {
                            return json(['code' => -1, 'message' => $goods_name . '库存不足，已抢购完毕']);
                        }
                        //新建一个key用来存用户购买某个sku的总数,用来判断用户总共购买的商品sku不超过限购数。
                        /*$user_buy_sku_num_key = 'buy_' . $seckill_id . '_' . $uid . '_' . $sku_id . '_num';
                        $user_buy_sku_num = $redis->get($user_buy_sku_num_key);
                        $new_total_num = $user_buy_sku_num + $num;
                        $redis->set($user_buy_sku_num_key, $new_total_num);*/
                    }
                    /*else {
//                      先关掉提示
                        return json(['code' => -1, 'message' => $goods_name . '秒杀活动已过期，更改为普通商品，请重新下单']);
                    }*/

                    //是否积分抵扣
                    $point_deduction = $order_business->pointDeductionOrder($v['sku'],$order_data['is_deduction'],$order_data['shipping_type']);
                  
                    $deduction_money = $point_deduction['total_deduction_money'];
                    $order_data['deduction_point'] = $point_deduction['total_deduction_point'];
                    if($order_data['shop'][$shop_id]['shop_should_paid_amount']>0){
                        $order_data['shop'][$shop_id]['shop_should_paid_amount'] = $order_data['shop'][$shop_id]['shop_should_paid_amount'] - $deduction_money;
                    }
                    $order_data['order'][$shop_id]['sku'] = $point_deduction['sku_info'];
                }
            }
            $redis_seckill_order_key = 'vslai_seckill_order';
            // 获取支付编号
//            echo '<pre>';print_r($order_data);exit;
            $out_trade_no = $order_service->getOrderTradeNo();
            $order_data['out_trade_no'] = $out_trade_no;
            $order_data['ip'] = $ip;
            $order_data['create_time'] = time();
            $order_data['order_from'] = $order_from;
            $order_data['website_id'] = $this->website_id;
            $seckill_shop_order_data = serialize([$uid => $order_data]);
            $is_redis_data = $redis->rpush($redis_seckill_order_key, $seckill_shop_order_data);
            //组支付需要的数据
            $all_should_paid_amount = 0;
            foreach ($order_data['shop'] as $k => $money) {
                $all_should_paid_amount += $money['shop_should_paid_amount'];
            }
            $pay_info = serialize(['out_trade_no' => $out_trade_no, 'create_time' => $order_data['create_time'], 'all_should_paid_amount' => $all_should_paid_amount]);
            $redis->set($out_trade_no, $pay_info);
            if($is_redis_data){
                $cookie_set_data['create_time'] = time();
                $cookie_set_data['out_trade_no'] = $out_trade_no;
                $cookie_set_data = serialize($cookie_set_data);
                Cookie::set($is_order_key, $cookie_set_data, 15);
            }
            $message['code'] = 0;
            $message['message'] = "订单提交成功";
            $message['data']['out_trade_no'] = $out_trade_no;
            return json($message);
        }

        $buyer_info = $user_model::get($this->uid);

        // 获取支付编号
        $out_trade_no = $order_service->getOrderTradeNo();

        $member = new Member();
        $shipping_time = time();
        $address = $member->getMemberExpressAddressDetail($order_data['address_id']);
        foreach ($order_data['order'] as $shop_id => $v) {
            $order_info = [];
            if($this->is_shop){
                $shop_model = new VslShopModel();
                $shop_info = $shop_model::get(['shop_id' => $shop_id, 'website_id' => $this->website_id]);
                $order_info['shop_name'] = $shop_info['shop_name'];
            }else{
                $order_info['shop_name'] = '自营店';
            }
            //自定义表单数据
            $order_info['custom_order'] = $post_data['custom_order'];
            $order_info['website_id'] = $this->website_id;
            $order_info['shop_id'] = $shop_id;

            $order_info['order_from'] = $order_from;
            if ($order_data['pay_type'] == 5) {
                $order_info['pay_money'] = 0;
                $order_info['user_platform_money'] = $order_data['shop'][$shop_id]['shop_should_paid_amount'];
            } else {
                $order_info['pay_money'] = $order_data['shop'][$shop_id]['shop_should_paid_amount'];
                $order_info['user_platform_money'] = 0;
            }
            //计算渠道商的金额 因为要将渠道商的金额分离，加到渠道商的账户里面
            if ($order_data['shop'][$shop_id]['shop_discount_amount'] == 0) {
                $order_info['channel_money'] = 0;
            } else {
                $order_info['channel_money'] = ($order_data['shop'][$shop_id]['shop_channel_amount'] / $order_data['shop'][$shop_id]['shop_discount_amount']) * $order_data['shop'][$shop_id]['shop_should_paid_amount'];
            }

            $order_info['normal_money'] = $order_data['shop'][$shop_id]['shop_should_paid_amount'] - $order_info['channel_money'];
            $order_info['user_money'] = 0;

            $order_info['member_money'] = $order_data['shop'][$shop_id]['member_amount'];
            $order_info['coupon_reduction_amount'] = $order_data['promotion'][$shop_id]['coupon']['coupon_reduction_amount'] ?: 0;
            $order_info['shop_total_amount'] = $order_data['shop'][$shop_id]['shop_total_amount'];
            $order_info['shop_should_paid_amount'] = $order_data['shop'][$shop_id]['shop_should_paid_amount'];
            $order_info['promotion_money'] = $order_data['shop'][$shop_id]['man_song_amount'] ?: 0;
            $order_info['order_money'] = $order_info['pay_money'] + $order_info['user_platform_money'];
            $order_info['shipping_fee'] = $order_data['shop'][$shop_id]['shipping_fee'] + $order_data['shop'][$shop_id]['promotion_free_shipping'];
            $order_info['promotion_free_shipping'] = $order_data['shop'][$shop_id]['promotion_free_shipping'];

            $order_info['platform_promotion_money'] = 0;
            $order_info['shop_promotion_money'] = 0;
            //post_data['shop'][shop_id]['man_song_amoun;t']
            if ($order_data['shop'][$shop_id]['man_song_amount']) {
                if ($order_data['shop'][$shop_id]['man_song_shop_id'] == 0) {
                    //平台优惠总额
                    $order_info['platform_promotion_money'] += $order_data['shop'][$shop_id]['man_song_amount'];
                    //('promotion_money:',$order_data['shop'][$shop_id]['man_song_amount']);
                } elseif ($order_data['shop'][$shop_id]['man_song_shop_id']) {
                    //店铺优惠总额
                    $order_info['shop_promotion_money'] += $order_data['shop'][$shop_id]['man_song_amount'];
                }
            }
            if ($order_data['coupon_reduction_amount']) {
                if ($order_data['promotion'][$shop_id]['coupon']['coupon_shop_id'] == 0) {
                    //平台优惠总额
                    $order_info['platform_promotion_money'] += $order_info['coupon_reduction_amount'];
                } elseif ($order_data['promotion'][$shop_id]['coupon']['coupon_shop_id']) {
                    //店铺优惠总额
                    $order_info['shop_promotion_money'] += $order_info['coupon_reduction_amount'];
                }
            }

            //post_data['shop'][shop_id]['man_song_shipping_shop_id']
            //post_data['shop'][shop_id]['promotion_free_shipping']
            if ($order_data['shop'][$shop_id]['promotion_free_shipping']) {
                if ($order_data['shop'][$shop_id]['man_song_shipping_shop_id'] == 0) {
                    //平台优惠总额
                    $order_info['platform_promotion_money'] += $order_data['shop'][$shop_id]['promotion_free_shipping'];
                    //var_dump('shipping:',$order_data['shop'][$shop_id]['promotion_free_shipping']);
                } elseif ($order_data['shop'][$shop_id]['man_song_shipping_shop_id']) {
                    //店铺优惠总额
                    $order_info['shop_promotion_money'] += $order_info['shop'][$shop_id]['promotion_free_shipping'];
                }
            }

            //post_data['order'][shop_id]['sku'][temp_sku_id]['promotion_shop_id']
            $presell_id = '';
            foreach ($v['sku'] as $sku_id => $sku_info) {
                $order_info['bargain_id'] = $sku_info['bargain_id'] ?: 0;
                if ($sku_info['discount_id'] || ($sku_info['member_price'] != $sku_info['price'])) {
                    //存在限时折扣或者平台会员价
                    if ($sku_info['promotion_shop_id'] == 0) {
                        //平台优惠总额 会员折扣也是
                        $order_info['platform_promotion_money'] += round(($sku_info['price'] - $sku_info['discount_price']) * $sku_info['num'], 2);
                        //var_dump('discount:',round(($sku_info['member_price'] - $sku_info['discount_price']) * $sku_info['num'], 2));
                    } elseif ($sku_info['promotion_shop_id']) {
                        //店铺优惠总额
                        $order_info['shop_promotion_money'] += round(($sku_info['member_price'] - $sku_info['discount_price']) * $sku_info['num'], 2);
                    }
                }
                //判断是否有预售
//                p($sku_info);exit;
                if (!empty($sku_info['presell_id'])) {
                    $presell_id = $sku_info['presell_id'];
                    //如果是预售的商品，则更改其单价为预售价
                    $presell_mdl = new VslPresellModel();
                    $presell_condition['p.id'] = $presell_id;
                    $presell_condition['pg.sku_id'] = $sku_info['sku_id'];
                    $presell_goods_info = $presell_mdl->alias('p')->where($presell_condition)->join('vsl_presell_goods pg', 'p.id = pg.presell_id', 'LEFT')->find();
                    if($presell_goods_info){
                        $v['sku'][$sku_id]['price'] = $presell_goods_info['allmoney'];
                        $v['sku'][$sku_id]['discount_price'] = $presell_goods_info['allmoney'];
                        $out_trade_no2 = $order_service->getOrderTradeNo();
                        $order_info['out_trade_no_presell'] = $out_trade_no2;
                        $order_info['final_money'] = $presell_goods_info['allmoney']*$sku_info['num'] - $order_info['pay_money'] + $order_info['shipping_fee'];
                    }
                }
                //计时/次商品
                if($sku_info['card_store_id']>0){
                    $order_info['card_store_id'] = $sku_info['card_store_id'];
                }
            }

            if ($order_info['pay_money'] != 0) {
                $order_info['order_status'] = 0;
                $order_info['pay_status'] = 0;
            } else {
                $order_info['order_status'] = 1;
                $order_info['pay_status'] = 2;
            }
            if ($post_data['order_type']) {
                $order_info['order_type'] = $post_data['order_type'];
            } else if ($order_data['group_id']) {
                $order_info['order_type'] = 5;
            }else if ($order_data['bargain_id']) {
                $order_info['order_type'] = 8;//砍价订单
            } else if($presell_id){
                $order_info['order_type'] = 7;//预售订单
            } else {
                $order_info['order_type'] = 1;
            }
            if($post_data['order_type']==2){
                $order_info['order_type'] = 2;
            }
            if($post_data['shopkeeper_id'] && $post_data['order_type']!=3 && $post_data['order_type']!=4 && $post_data['order_type']!=2){
                $order_info['shopkeeper_id'] = $post_data['shopkeeper_id'];
            }
            if($post_data['order_type']==2){
                $order_info['order_type'] = 2;//成为店主
            }
            if($post_data['shopkeeper_id'] && $post_data['order_type']==3){
                $order_info['shopkeeper_id'] = $post_data['shopkeeper_id'];
                $order_info['order_type'] = 3;//店主续费
            }
            if($post_data['shopkeeper_id'] && $post_data['order_type']==4){
                $order_info['shopkeeper_id'] = $post_data['shopkeeper_id'];
                $order_info['order_type'] = 4;//店主升级
            }
            if($order_data['shop'][$shop_id]['store_id'] || $order_data['shop'][$shop_id]['card_store_id']){
                $store = new Store();
                if($order_data['shop'][$shop_id]['store_id']){
                    $store_info = $store->storeDetail($order_data['shop'][$shop_id]['store_id']);
                    $order_info['store_id'] = $order_data['shop'][$shop_id]['store_id'];
                    $order_info['verification_code'] = $store->createVerificationCode();
                }else{
                    $store_info = $store->storeDetail($order_data['shop'][$shop_id]['card_store_id']);
                }
                $address['province'] = $store_info['province_id'];
                $address['city'] = $store_info['city_id'];
                $address['district'] = $store_info['district_id'];
                $address['address'] = $store_info['address'];
            }
            $order_info['out_trade_no'] = $out_trade_no;
            $order_info['order_sn'] = $out_trade_no;
            $order_info['order_no'] = $order_business->createOrderNo($shop_id);
            $order_info['pay_type'] = "0";
            $order_info['shipping_type'] = ($order_data['shop'][$shop_id]['store_id'] || $order_data['shop'][$shop_id]['card_store_id']) ? $order_data['shipping_type'] : 1;
            $order_info['ip'] = $ip;
            $order_info['leave_message'] = $order_data['shop'][$shop_id]['leave_message'] ?: '';
            $order_info['buyer_invoice'] = '';
            $order_info['shipping_time'] = $shipping_time;
            $order_info['receiver_mobile'] = isset($address['mobile'])?$address['mobile']:'';
            $order_info['receiver_province'] = isset($address['province'])?$address['province']:'';
            $order_info['receiver_city'] = isset($address['city'])?$address['city']:'';
            $order_info['receiver_district'] = isset($address['district'])?$address['district']:'';
            $order_info['receiver_address'] = isset($address['address'])?$address['address']:'';
            $order_info['receiver_zip'] = isset($address['zip_code'])?$address['zip_code']:'';
            $order_info['receiver_name'] = isset($address['consigner'])?$address['consigner']:'';
            $order_info['sku_info'] = $v['sku'];
            $order_info['pick_up_id'] = 0;
            $order_info['create_time'] = time();

            $order_info['buyer_id'] = $this->uid;
            $order_info['nick_name'] = $buyer_info['nick_name'];
            $order_info['group_id'] = $order_data['group_id'];
            $order_info['presell_id'] = $presell_id;
            $order_info['group_record_id'] = $order_data['record_id'];//如果参加了团，订单标记是哪个团
            
            if (!empty($order_data['promotion'][$shop_id]['man_song'])) {
                foreach ($order_data['promotion'][$shop_id]['man_song'] as $man_song_id => $man_song_info) {
                    $order_info['man_song_full_cut'][$man_song_id]['rule_id'] = $man_song_info['rule_id'];
                    $order_info['man_song_full_cut'][$man_song_id]['price'] = $man_song_info['price'];
                    $order_info['man_song_full_cut'][$man_song_id]['discount'] = $man_song_info['discount'];

                    if ($man_song_info['free_shipping_fee'] == true) {
                        if ($man_song_info['shop_id'] == 0) {
                            //平台优惠总额
                            $order_info['platform_promotion_money'] += $order_info['shipping_fee'];
                        } elseif ($man_song_info['shop_id']) {
                            //店铺优惠总额
                            $order_info['shop_promotion_money'] += $order_info['shipping_fee'];
                        }
                    }
                }
            }
//            p($order_data);exit;
            if (!empty($order_data['promotion'][$shop_id]['coupon'])) {
                $order_info['coupon']['coupon_id'] = $order_data['promotion'][$shop_id]['coupon']['coupon_id'];
                $order_info['coupon']['discount'] = $order_data['promotion'][$shop_id]['coupon']['coupon_reduction_amount'];//优惠券优惠多少钱
                $order_info['coupon']['coupon_genre'] = $order_data['promotion'][$shop_id]['coupon']['coupon_genre'];
                $order_info['coupon']['money'] = $order_data['promotion'][$shop_id]['coupon']['money'];//优惠券设置的满减金额
                $order_info['coupon']['coupon_discount'] = $order_data['promotion'][$shop_id]['coupon']['discount'];//优惠券设置折扣
                $order_info['coupon']['price'] = $order_data['promotion'][$shop_id]['coupon']['at_least'];
            }

            //积分抵扣
            $point_deduction = $order_business->pointDeductionOrder($order_info['sku_info'],$order_data['is_deduction'],$order_info['shipping_type']);
            $order_info['deduction_money'] = $point_deduction['total_deduction_money'];
            $order_info['deduction_point'] = $point_deduction['total_deduction_point'];
            $order_info['sku_info'] = $point_deduction['sku_info'];
            
            
            if($order_info['deduction_money']>0){
                if(!empty($order_info['sku_info'][0]['presell_id'])){//预售处理
                    $order_info['final_money'] = $order_info['final_money'] - $order_info['deduction_money'];
                    if($order_info['final_money']<=0){
                        $order_info['order_money'] = $order_info['order_money'] + $order_info['final_money'];
                        if($order_info['pay_money']>0){
                            $order_info['pay_money'] = $order_info['pay_money'] + $order_info['final_money'];
                            $order_info['final_money'] = 0;
                        }
                        if($order_info['user_platform_money']>0){
                            $order_info['user_platform_money'] = $order_info['user_platform_money'] + $order_info['final_money'];
                            $order_info['final_money'] = 0;
                        }
                    }
                    if ($order_info['pay_money'] == 0 && $order_info['final_money'] == 0) {
                        $order_info['money_type'] = 2;
                    }
                }else{
                    if($order_info['pay_money']>0){
                        $order_info['pay_money'] = $order_info['pay_money'] - $order_info['deduction_money'];
                    }
                    if($order_info['user_platform_money']>0){
                        $order_info['user_platform_money'] = $order_info['user_platform_money'] - $order_info['deduction_money'];
                    }
                    $order_info['order_money'] = $order_info['order_money'] - $order_info['deduction_money'];
                }
                if ($order_info['pay_money'] != 0) {
                    $order_info['order_status'] = 0;
                    $order_info['pay_status'] = 0;
                } else {
                    $order_info['order_status'] = 1;
                    $order_info['pay_status'] = 2;
                }
            }
            $order_business = new OrderBusiness();
            $order_id = $order_business->orderCreateNew($order_info);

            $config = new \data\service\Config();
            $shopConfig = $config->getShopConfig(0);
            // 针对特殊订单执行支付处理
            if ($order_id > 0 && is_numeric($order_id)) {
                if ($order_info['pay_money'] > 0) {
                    // 还需要支付的订单发送已创建待支付订单短信 邮件通知
                    // 当创建多店铺订单时阿里云短信发送会将市区设为GMT，所以手动设为PRC
                    date_default_timezone_set("PRC");
                    $timeout = date('Y-m-d H:i:s', $order_info['create_time'] + ($shopConfig['order_buy_close_time'] * 60));
                    runhook('Notify', 'orderCreateBySms', array('order_id' => $order_id, 'time_out' => $timeout));
                    runhook('Notify', 'emailSend', ['website_id' => $this->website_id, 'shop_id' => $this->instance_id, 'order_id' => $order_id, 'notify_type' => 'user', 'time_out' => $timeout, 'template_code' => 'create_order']);
                }
                $order_model = new VslOrderModel();

                $order_info = $order_model->getInfo(['order_id' => $order_id], '*');
                if (!empty($order_info)) {
                    if ($order_info['user_platform_money'] != 0) {
                        if ($order_info['pay_money'] == 0) {
                            $order_service->orderOnLinePay($order_info['out_trade_no'], 5, $order_id);
                        }
                    } else {
                        if ($order_info['pay_money'] == 0) {
                            $order_service->orderOnLinePay($order_info['out_trade_no'], 1, $order_id); // 默认微信支付
                        }
                    }
                }
            } else {
                return AjaxReturn($order_id);
            }
        }
        if ($order_id > 0) {
            if (Session::get('order_tag') == 'cart'){
                $sku_id_array = [];
                foreach ($order_data['order'] as $shop_id => $goods) {
                    foreach ($goods['sku'] as $sku) {
                        $sku_id_array[] = $sku['sku_id'];
                    }
                }
                $delete_cart_condition['buyer_id'] = $this->uid;
                $delete_cart_condition['sku_id'] = ['IN', $sku_id_array];
                $order_service->deleteCartNew($delete_cart_condition);
            }
            $cookie_set_data['create_time'] = time();
            $cookie_set_data['out_trade_no'] = $out_trade_no;
            $cookie_set_data['order_id'] = $order_id;
            $cookie_set_data = serialize($cookie_set_data);
            Cookie::set($is_order_key, $cookie_set_data, 15);
            $message['code'] = 0;
            $message['message'] = "订单提交成功";
            $message['data']['out_trade_no'] = $out_trade_no;
            return json($message);
        } else {
            $message['code'] = -1;
            $message['message'] = "订单提交失败";
            $message['data'] = '';
            return json($message);
        }

    }

    /*
     * 判断是否有秒杀商品
     * **/
    public function isSeckillOrder($order_shop)
    {
        if($this->is_seckill){
            $sec_service = new seckillServer();
            $order_type = '';
            foreach ($order_shop as $shop_id => $v) {
                foreach ($v['sku'] as $sku => $sku_list) {
                    $sku_id = $sku_list['sku_id'];
                    $seckill_id = $sku_list['seckill_id'];
                    $condition_is_seckill['s.seckill_id'] = $seckill_id;
                    $condition_is_seckill['nsg.sku_id'] = $sku_id;
                    $is_seckill = $sec_service->isSeckillGoods($condition_is_seckill);
                    if ($is_seckill) {
                        $order_type = 'seckill_order';
                    }
                }
            }
        }
        return $order_type;
    }

    //获取订单结果
    public function get_pay_result_info()
    {
        $order = new OrderService();
        $member_recharge = new VslMemberRechargeModel();
        $payment = new VslOrderPaymentModel();
        $out_trade_no = request()->post('out_trade_no', '');
        if (empty($out_trade_no)) {
            $data['code'] = -1;
            $data['message'] = "外部交易号不能为空";
            return json($data);
        }
        if(strstr($out_trade_no, 'eos')){
            $data['code'] = 0;
            $block = new Block();
            $status = $block->checkOrderStatus($out_trade_no);
            if($status==1){
                $data['data']['pay_status'] = 2;
            }else{
                $data['data']['pay_status'] = 0;
            }
            return json($data);
        }else{
            $condition['out_trade_no'] = $out_trade_no;
            $recharge_info = $member_recharge->getInfo($condition);
            $info = $order->get_status_by_outno($out_trade_no);
            $orderId = $order->getOrderIdByOutno($out_trade_no);//获取订单id，多单返回0；
            $payment_info = $payment->getInfo($condition);
            if(strstr($out_trade_no, 'QD')){
                $info = $order->getChannelStatusByOutno($out_trade_no);
            }
            //计时/次商品使用门店信息
            $card_store = [];
            $card_store['shop_name'] = "";
            $card_store['store_name'] = "";
            $card_store['store_tel'] = "";
            $card_store['address'] = "";
            if (!empty($info)) {
                $data['code'] = 0;
                if($info['money_type'] == 1){//预售第一次支付成功
                    $data['data']['pay_status'] = 2;
                }else{
                    $data['data']['pay_status'] = $info['pay_status'];
                }
                $data['data']['presell_id'] = $info['presell_id'];  //预售ID
                $data['data']['is_integral_order'] = $info['order_type'] == 10 ? 1: 0;  //预售ID
                $data['data']['group_id'] = $info['group_id'];      //拼团活动ID
                $data['data']['group_record_id'] = $info['group_record_id'];    //拼团记录ID
                $data['data']['pay_gift_status'] = $info['pay_gift_status'];    //支付有礼状态
                if(strstr($out_trade_no, 'QD')){
                    $data['data']['is_channel'] = 'purchase';
                }else{
                    $data['data']['is_channel'] = $info['buy_type'] == 2 ? 'pickupgoods' : '';
                }
                $wx_card_state = 0;
                if($info['card_store_id']>0){
                    $member_card = new VslMemberCardModel();
                    if($info['card_ids']){
                        $card_ids = explode(',',$info['card_ids']);
                        foreach ($card_ids as $k => $v) {
                            $card_info = $member_card->getDetail(['vmc.card_id'=>$v]);
                            if($card_info['wx_card_state']==1 || $card_info['card_type']==1)$wx_card_state = 1;
                        }
                    }
                    $storeServer = new Store();
                    $store_info = $storeServer->storeDetail($info['card_store_id']);
                    $card_store['shop_name'] = $store_info['shop_name'];
                    $card_store['store_name'] = $store_info['store_name'];
                    $card_store['store_tel'] = $store_info['store_tel'];
                    $card_store['address'] = $store_info['detailed_address'];
                }
                $data['data']['card_store'] = $card_store;  //计时/次商品使用门店信息
                $data['data']['wx_card_state'] = $wx_card_state;  //计时/次商品微信卡券领取状态
                $data['data']['card_ids'] = $info['card_ids']; //计时/次商品微信卡券领取id
                $data['data']['order_type'] = $info['order_type']; //订单类型
                $data['data']['shipping_type'] = $info['shipping_type']; //配送类型
                $data['data']['order_from'] = 1; //商城订单
                $data['data']['order_id'] = $orderId; //订单id
                return json($data);
            } elseif(!empty($recharge_info)) {
                $data['code'] = 0;
                if($recharge_info['is_pay']==1){
                    $data['data']['pay_status'] = 2;
                }else{
                    $data['data']['pay_status'] = 0;
                }
                $data['data']['presell_id'] = '';  //预售ID
                $data['data']['group_id'] = '';      //拼团活动ID
                $data['data']['group_record_id'] = '';    //拼团记录ID
                $data['data']['pay_gift_status'] = '';    //支付有礼状态
                $data['data']['is_channel'] = '';
                $data['data']['card_store'] = $card_store; //计时/次商品使用门店信息
                $data['data']['wx_card_state'] = ''; //计时/次商品微信卡券领取状态
                $data['data']['card_ids'] = ''; //计时/次商品微信卡券领取id
                $data['data']['order_type'] = 1; //订单类型
                $data['data']['order_from'] = 2; //充值订单
                $data['data']['shipping_type'] = 1;
                $data['data']['order_id'] = 0; //订单id
                return json($data);
            }else if(!empty($payment_info)){
                $data['code'] = 0;
                $data['data']['pay_status'] = $payment_info['pay_status'];
                $data['data']['presell_id'] = '';  //预售ID
                $data['data']['group_id'] = '';      //拼团活动ID
                $data['data']['group_record_id'] = '';    //拼团记录ID
                $data['data']['pay_gift_status'] = '';    //支付有礼状态
                $data['data']['is_channel'] = '';
                $data['data']['card_store'] = $card_store; //计时/次商品使用门店信息
                $data['data']['wx_card_state'] = ''; //计时/次商品微信卡券领取状态
                $data['data']['card_ids'] = ''; //计时/次商品微信卡券领取id
                $data['data']['order_type'] = 1; //订单类型
                $data['data']['order_from'] = 1; //商城订单
                $data['data']['shipping_type'] = 1;
                $data['data']['order_id'] = 0; //订单id
                return json($data);
            }else{
                return json(['code' => -1, 'message' => '错误的交易号']);

            }
        }

    }

    /**
     *
     * 创建秒杀订单
     */
    public function seckillOrderCreate($redis_order_data)
    {
        $order_service = new OrderService();
        $order_business = new OrderBusiness();
        $user_model = new UserModel();
        $order_data_arr = unserialize($redis_order_data);
        $uid = array_keys($order_data_arr)[0];
        $order_data = array_values($order_data_arr)[0];
        $buyer_info = $user_model::get($uid);
        $order_from = $order_data['order_from']; // 手机
        // 获取支付编号
        $out_trade_no = $order_data['out_trade_no'];
        $member = new Member();
        $shipping_time = time();
        $ip = $order_data['ip'];
        $website_id = $order_data['website_id'];
        $create_time = $order_data['create_time'];
        if ($order_data['address_id']) {
            $address = $member->getMemberExpressAddressDetail($order_data['address_id'], $uid);
        }
        $err = 0;
        $count_shop = count($order_data['order']);
        foreach ($order_data['order'] as $shop_id => $v) {
            if($this->is_shop){
                $shop_model = new VslShopModel();
                $shop_info = $shop_model::get(['shop_id' => $shop_id, 'website_id' => $this->website_id]);
            }else{
                $shop_info['shop_name'] = '自营店';
            }
            $order_info = [];
            $order_info['website_id'] = $website_id;
            $order_info['shop_id'] = $shop_id;
            $order_info['shop_name'] = $shop_info['shop_name'];
            $order_info['order_from'] = $order_from;
            if ($order_data['pay_type'] == 5) {
                $order_info['pay_money'] = 0;
                $order_info['user_platform_money'] = $order_data['shop'][$shop_id]['shop_should_paid_amount'];
            } else {
                $order_info['pay_money'] = $order_data['shop'][$shop_id]['shop_should_paid_amount'];
                $order_info['user_platform_money'] = 0;
            }

            $order_info['user_money'] = 0;

            $order_info['member_money'] = $order_data['shop'][$shop_id]['member_amount'];//会员价
            $order_info['coupon_reduction_amount'] = $order_data['promotion'][$shop_id]['coupon']['coupon_reduction_amount'] ?: 0;//优惠券优惠金额
            $order_info['shop_total_amount'] = $order_data['shop'][$shop_id]['shop_total_amount'];//店铺总金额
            $order_info['shop_should_paid_amount'] = $order_data['shop'][$shop_id]['shop_should_paid_amount'];//店铺优惠后的总金额
            $order_info['promotion_money'] = $order_data['shop'][$shop_id]['man_song_amount'] ?: 0;//满送优惠金额 不包括满减运费
            $order_info['order_money'] = $order_info['pay_money'] + $order_info['user_platform_money'];//要支付的金额加上平台账户余额 必须有一个是0
            $order_info['shipping_fee'] = $order_data['shop'][$shop_id]['shipping_fee'];//
            $order_info['promotion_free_shipping'] = $order_data['shop'][$shop_id]['promotion_free_shipping'];//减掉满减的运费

            $order_info['platform_promotion_money'] = 0;//
            $order_info['shop_promotion_money'] = 0;//
            //post_data['shop'][shop_id]['man_song_amount']
            if ($order_data['shop'][$shop_id]['man_song_amount']) {
                if ($order_data['shop'][$shop_id]['man_song_shop_id'] == 0) {
                    //平台优惠总额
                    $order_info['platform_promotion_money'] += $order_data['shop'][$shop_id]['man_song_amount'];
                    //('promotion_money:',$order_data['shop'][$shop_id]['man_song_amount']);
                } elseif ($order_data['shop'][$shop_id]['man_song_shop_id']) {
                    //店铺优惠总额
                    $order_info['shop_promotion_money'] += $order_data['shop'][$shop_id]['man_song_amount'];
                }
            }
            if ($order_data['coupon_reduction_amount']) {
                if ($order_data['promotion'][$shop_id]['coupon']['coupon_shop_id'] == 0) {
                    //平台优惠券优惠总额
                    $order_info['platform_promotion_money'] += $order_info['coupon_reduction_amount'];
                } elseif ($order_data['promotion'][$shop_id]['coupon']['coupon_shop_id']) {
                    //店铺优惠券优惠总额
                    $order_info['shop_promotion_money'] += $order_info['coupon_reduction_amount'];
                }
            }

            //post_data['shop'][shop_id]['man_song_shipping_shop_id']
            //post_data['shop'][shop_id]['promotion_free_shipping']
            if ($order_data['shop'][$shop_id]['promotion_free_shipping']) {
                if ($order_data['shop'][$shop_id]['man_song_shipping_shop_id'] == 0) {
                    //平台运费优惠总额
                    $order_info['platform_promotion_money'] += $order_data['shop'][$shop_id]['promotion_free_shipping'];
                    //var_dump('shipping:',$order_data['shop'][$shop_id]['promotion_free_shipping']);
                } elseif ($order_data['shop'][$shop_id]['man_song_shipping_shop_id']) {
                    //店铺运费优惠总额
                    $order_info['shop_promotion_money'] += $order_info['shop'][$shop_id]['promotion_free_shipping'];
                }
            }

            //post_data['order'][shop_id]['sku'][temp_sku_id]['promotion_shop_id']
            foreach ($v['sku'] as $sku_id => $sku_info) {
                if ($sku_info['discount_id'] || ($sku_info['member_price'] != $sku_info['price'])) {
                    //存在限时折扣或者平台会员价
                    if ($sku_info['promotion_shop_id'] == 0) {
                        //平台优惠总额 会员折扣也是
                        $order_info['platform_promotion_money'] += round(($sku_info['price'] - $sku_info['discount_price']) * $sku_info['num'], 2);
                        //var_dump('discount:',round(($sku_info['member_price'] - $sku_info['discount_price']) * $sku_info['num'], 2));
                    } elseif ($sku_info['promotion_shop_id']) {
                        //店铺优惠总额
                        $order_info['shop_promotion_money'] += round(($sku_info['member_price'] - $sku_info['discount_price']) * $sku_info['num'], 2);
                    }
                }
            }

            if ($order_info['pay_money'] != 0) {
                $order_info['order_status'] = 0;
                $order_info['pay_status'] = 0;
            } else {
                $order_info['order_status'] = 1;
                $order_info['pay_status'] = 2;
            }

            $order_info['order_type'] = 6;
            if($order_data['shop'][$shop_id]['store_id']){
                $store = new Store();
                $store_info = $store->storeDetail($order_data['shop'][$shop_id]['store_id']);
                $address['province'] = $store_info['province_id'];
                $address['city'] = $store_info['city_id'];
                $address['district'] = $store_info['district_id'];
                $address['address'] = $store_info['address'];
                $address['mobile'] = '';
                $address['zip_code'] = '';
                $address['consigner'] = '';
                $order_info['store_id'] = $order_data['shop'][$shop_id]['store_id'];
                $order_info['verification_code'] = $store->createVerificationCode();
            }
            $order_info['out_trade_no'] = $out_trade_no;
            $order_info['order_sn'] = $out_trade_no;
            $order_info['order_no'] = $order_business->createOrderNo($shop_id);
            $order_info['pay_type'] = "0";
            $order_info['shipping_type'] = $order_data['shipping_type']?:1;
            $order_info['order_from'] = $order_from;
            $order_info['ip'] = $ip;
            $order_info['leave_message'] = $order_data['shop'][$shop_id]['leave_message'] ?: '';
            $order_info['buyer_invoice'] = '';
            $order_info['shipping_time'] = $shipping_time;
            $order_info['receiver_mobile'] = isset($address['mobile'])?$address['mobile']:'';
            $order_info['receiver_province'] = isset($address['province'])?$address['province']:'';
            $order_info['receiver_city'] = isset($address['city'])?$address['city']:'';
            $order_info['receiver_district'] = isset($address['district'])?$address['district']:'';
            $order_info['receiver_address'] = isset($address['address'])?$address['address']:'';
            $order_info['receiver_zip'] = isset($address['zip_code'])?$address['zip_code']:'';
            $order_info['receiver_name'] = isset($address['consigner'])?$address['consigner']:'';

            $order_info['sku_info'] = $v['sku'];
            $order_info['pick_up_id'] = 0;
            $order_info['create_time'] = $create_time;

            $order_info['buyer_id'] = $uid;
            $order_info['nick_name'] = $buyer_info['nick_name'];
//            post_data['promotion'][shop_id]['man_song'][temp_man_song_rule_id]['full_cut']['man_song_id']
//            post_data['promotion'][shop_id]['man_song'][temp_man_song_rule_id]['shipping']['man_song_info'] = true;
            if (!empty($order_data['promotion'][$shop_id]['man_song'])) {
                foreach ($order_data['promotion'][$shop_id]['man_song'] as $man_song_id => $man_song_info) {
                    $order_info['man_song_full_cut'][$man_song_id]['rule_id'] = $man_song_info['full_cut']['rule_id'];
                    $order_info['man_song_full_cut'][$man_song_id]['price'] = $man_song_info['full_cut']['price'];
                    $order_info['man_song_full_cut'][$man_song_id]['discount'] = $man_song_info['full_cut']['discount'];
                    if ($man_song_info['free_shipping_fee'] == true) {
                        if ($man_song_info['shop_id'] == 0) {
                            //平台优惠总额
                            $order_info['platform_promotion_money'] += $order_info['shipping_fee'];
                        } elseif ($man_song_info['shop_id']) {
                            //店铺优惠总额
                            $order_info['shop_promotion_money'] += $order_info['shipping_fee'];
                        }
                    }
                }
                //如果是秒杀商品，则将上面加上秒杀优惠的运费减掉。
                foreach ($v['sku'] as $sku_id => $sku_info) {
                    if ($man_song_info['shop_id'] == 0) {
                        if (!empty($sku_info['seckill_id'])) {
                            $order_info['platform_promotion_money'] -= $sku_info['shipping_fee'];
                        }
                    } elseif ($man_song_info['shop_id']) {
                        if (!empty($sku_info['seckill_id'])) {
                            $order_info['shop_promotion_money'] -= $sku_info['shipping_fee'];
                        }
                    }
                }
            }
            if (!empty($order_data['promotion'][$shop_id]['coupon'])) {
                $order_info['coupon']['coupon_id'] = $order_data['promotion'][$shop_id]['coupon']['coupon_id'];
                $order_info['coupon']['discount'] = $order_data['promotion'][$shop_id]['coupon']['coupon_reduction_amount'];//优惠券优惠多少钱
                $order_info['coupon']['coupon_genre'] = $order_data['promotion'][$shop_id]['coupon']['coupon_genre'];
                $order_info['coupon']['money'] = $order_data['promotion'][$shop_id]['coupon']['money'];//优惠券设置的满减金额
                $order_info['coupon']['coupon_discount'] = $order_data['promotion'][$shop_id]['coupon']['discount'];//优惠券设置折扣
                $order_info['coupon']['price'] = $order_data['promotion'][$shop_id]['coupon']['at_least'];
            }
            try {
                $order_business = new OrderBusiness();
                //            echo '<pre>';print_r($order_info);exit;
                $order_id = $order_business->orderCreateNew($order_info);
                //只要有异常就不能插入数据库，将队列订单数据请求压回去
                if ($order_id < 0) {
                    $err++;
                }
                //满减送积分 > 0 送积分
                if ($order_data['shop'][$shop_id]['man_song_point'] > 0) {
                    $memberAccount = new Member\MemberAccount();
                    $memberAccount->addMemberAccountData(1, $this->uid, 1, $order_data['shop'][$shop_id]['man_song_point'],
                        1, $this->website_id, '注册营销,注册得积分');
                }

                //满减送有设置优惠券 送优惠券
                if ($order_data['shop'][$shop_id]['man_song_coupon_type_id'] > 0) {
                    $coupon_server = new Coupon();
                    if ($coupon_server->isCouponTypeReceivable($order_data['shop'][$shop_id]['man_song_coupon_type_id'], $this->uid)) {
                        $coupon_server->userAchieveCoupon($this->uid, $order_data['shop'][$shop_id]['man_song_coupon_type_id'], 1);
                    }
                }

                $config = new \data\service\Config();
                $shopConfig = $config->getShopConfig(0, $website_id);
                // 针对特殊订单执行支付处理
                if ($order_id > 0) {
                    foreach ($v['sku'] as $sku_id => $sku_info) {
                        if (!empty($sku_info['seckill_id'])) {
                            $redis = $this->connectRedis();
                            $num = $sku_info['num'];
                            //新建一个key用来存用户购买某个sku的总数,用来判断用户总共购买的商品sku不超过限购数。
                            $user_buy_sku_num_key = 'buy_' . $sku_info['seckill_id'] . '_' . $uid . '_' . $sku_info['sku_id'] . '_num';
                            $user_buy_sku_num = $redis->get($user_buy_sku_num_key);
                            $new_total_num = $user_buy_sku_num + $num;
                            $redis->set($user_buy_sku_num_key, $new_total_num);
                        }
                    }
                    if ($order_info['pay_money'] > 0) {
                        // 还需要支付的订单发送已创建待支付订单短信 邮件通知
//                        date_default_timezone_set('PRC');
                        $timeout = date('Y-m-d H:i:s', $order_info['create_time'] + ($shopConfig['order_buy_close_time'] * 60));
                        runhook('Notify', 'orderCreateBySms', array('order_id' => $order_id, 'time_out' => $timeout));
                        runhook('Notify', 'emailSend', ['website_id' => $website_id, 'shop_id' => 0, 'order_id' => $order_id, 'notify_type' => 'user', 'time_out' => $timeout, 'template_code' => 'create_order']);
                    }
                    $order_model = new VslOrderModel();

                    $order_info = $order_model->getInfo(['order_id' => $order_id], '*');
                    if (!empty($order_info)) {
                        if ($order_info['user_platform_money'] != 0) {
                            if ($order_info['pay_money'] == 0) {
                                $order_service->orderOnLinePay($order_info['out_trade_no'], 5, $order_id);
                            }
                        } else {
                            if ($order_info['pay_money'] == 0) {
                                $order_service->orderOnLinePay($order_info['out_trade_no'], 1, $order_id); // 默认微信支付
                            }
                        }
                    }
                    //如果$order_id存在，则删除购物车的东西
                    foreach($v['sku'] as $k1=>$v1){
                        $sku_id_array[] = $v1['sku_id'];
                    }
                    $delete_cart_condition['buyer_id'] = $uid;
                    $delete_cart_condition['sku_id'] = ['IN', $sku_id_array];
                    $order_service->deleteCartNew($delete_cart_condition);
                }
            } catch (\Exception $e) {
                $e->getMessage();
            }

//            Log::write($order_id);

        }
        if ($err == $count_shop) {
            return ['status' => false, 'out_trade_no' => $out_trade_no];
        } else {
            return ['status' => true, 'out_trade_no' => $out_trade_no];
        }
    }
    /**
     *
     * 获取物流公司列表
     * page_index 页数
     * page_size 页数量
     * search_text 公司名
     */
    public function getvExpressCompany(){
        $page_index = request()->post('page_index', 1);
        $page_size = request()->post('page_size', PAGESIZE);
        $search_text = request()->post('search_text', '');

        $orderby = 'no.is_default desc,no.id desc';
        $group = '';
        $field = 'sp.company_name,sp.co_id';
        
        $condition['no.website_id'] = $this->website_id;
        $condition['no.shop_id'] = $this->instance_id;
        
        if($search_text){
            $condition['sp.company_name'] = array('like','%'.$search_text.'%');
            
        }
        
        $comShopRela = new VslExpressCompanyShopRelationModel();
        $express_all = $comShopRela->getExpressQuery($page_index, $page_size, $condition,$orderby,$group,$field);
        $page_count = $express_all['page_count'];
        $total_count = $express_all['total_count'];
        $expressList = array();
        foreach ($express_all['data'] as $v) {
            if($v['company_name'] && $v['co_id']){
                $rdata = array(
                    'company_name' => $v['company_name'],
                    'co_id' => $v['co_id'],
                );
                array_push($expressList,$rdata); 
            }else{
                $total_count -= 1;
            }   
        }
        $data['code'] = 1;
        $data['data']['expressList'] = $expressList;
        $data['data']['page_count'] = $page_count;
        $data['data']['total_count'] = $total_count;
        $data['message'] = "获取成功";
        return json($data);
    }
}